how to compress image in iphone?

后端 未结 3 712
离开以前
离开以前 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:46

    UIImageJPEGRepresentation(UIImage,Quality);

    1.0 means maximum Quality and 0 means minimum quality.

    SO change the quality parameter in below line to reduce file size of the image

    NSData* data = UIImageJPEGRepresentation(image,1.0);
    
    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2020-12-15 14:55
    NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);
    

    OR

    NSData *image_Data=UIImageJPEGRepresentation(image_Name,compressionQuality);
    

    return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compressionQuality is 0(most) & 1(least).

    0 讨论(0)
提交回复
热议问题