Monish Kumar Monish Kumar - 19 days ago 6
Objective-C Question

How to compress an Image taken by the camera in iphone sdk?

I am getting the list of contacts from the Address book in that some contacts have images which are taken with camera they are very huge in size.I am displaying the contacts along with their images in a 3x3 rows and columns format.The problem is due to huge size of the images its taking time to load the images.Can anyone suggest me how to Compress them.
I tried to compress them in a way:

if ([imageData length] > 0)
{
int len = [imageData length];
if(len > 9000)
{
UIImage *theImage = [UIImage imageWithData:imageData];
imageData = UIImageJPEGRepresentation(theImage,0.5);
printf("\n image data length in condition...%d",[imageData length]);
imageViewL.image = [UIImage imageWithData:imageData];
}
else
{
imageViewL.image = [UIImage imageWithData:imageData];
}
}


Eventhough its taking time to load.

Anyone's help will be much appreciated.

Thanks to all,
Monish.

Answer

You can resize the image captured by the iphone camera by using the following lines of code

-(UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)newSize
{  
    UIGraphicsBeginImageContext(newSize);  
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];  
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();    
    return newImage;  
}

and then call this method like this

UIImage *image = [self scaleImage:your image toSize:CGSizeMake(320.0,480.0)];