How to compress image in iphone?

I accept images from the photo library. I have large images 4-5 mb, but I want to compress these images. Since I need to store these images in iphone.for local memory using less memory or to get less memory warning I need to compress these images.

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

    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 don't want to keep the whole image too big. So I want to shrink it to a much smaller size, as I will need to add multiple images.

Thanks for the answer.

+3


source to share


3 answers


You can choose a lower quality for JPEG encoding

NSData* data = UIImageJPEGRepresentation(image, 0.8);

      

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



On top of this, review resizing the image before creating the JPEG view 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: Easiest way to resize UIImage?

+11


source


UIImageJPEGRepresentation (UIImage, quality);

1.0 means maximum quality and 0 means minimum quality.



SO change the quality setting on the bottom line to reduce the size of the image file

NSData* data = UIImageJPEGRepresentation(image,1.0);

      

+5


source


NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);

      

OR

NSData *image_Data=UIImageJPEGRepresentation(image_Name,compressionQuality);

      

return a JPEG image. May return zero if the image does not have a CGImageRef or an invalid bitmap format. compressionQuality

is equal to 0 (most) and 1 (smallest).

+1


source







All Articles