Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compress image in iphone?

I m taking images from photo library.I have large images of 4-5 mb but i want to compress those images.As i need to store those images in local memory of iphone.for using less memory or for getting less memory warning i need to compress those images.

I don't know how to compress images and videos.So i want to know hot to compress images?

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    NSData* data = UIImageJPEGRepresentation(image,1.0);
    NSLog(@"found an image");

    NSString *path = [destinationPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpeg", name]];
    [data writeToFile:path atomically:YES]; 

This is the code for saving my image. I dont want to store the whole image as its too big. So, I want to compress it to a much smaller size as I'll need to attach multiple images.

Thanks for the reply.

like image 395
nids Avatar asked Dec 02 '22 00:12

nids


1 Answers

You can choose a lower quality for JPEG encoding

NSData* data = UIImageJPEGRepresentation(image, 0.8);

Something like 0.8 shouldn't be too noticeable, and should really improve file sizes.

On top of this, look into resizing the image before making the JPEG representation, using a method like this:

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

Source: The simplest way to resize an UIImage?

like image 148
joerick Avatar answered Dec 07 '22 01:12

joerick