how to compress image in iphone?

后端 未结 3 711
离开以前
离开以前 2020-12-15 14:14

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 le

3条回答
  •  既然无缘
    2020-12-15 14:53

    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?

提交回复
热议问题