Monish Kumar Monish Kumar - 1 year ago 55
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];
imageViewL.image = [UIImage imageWithData:imageData];

Eventhough its taking time to load.

Anyone's help will be much appreciated.

Thanks to all,

Answer Source

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

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

and then call this method like this

UIImage *image = [self scaleImage:your image toSize:CGSizeMake(320.0,480.0)];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download