reduce camera/photo library image file size for less than 100 KB in iphone

假如想象 提交于 2019-12-20 10:47:26

问题


I want to reduce image file size that take from UIImagePickerController . I use this method

NSData *imageData = UIImageJPEGRepresentation(image, 0.1);

but it reduce 2.2 MB image file size to 300 KB I want my image file size become less than 100 KB.


回答1:


Easiest way to reduce image size in kilos is to reduce the size in pixels! Scale it smaller:

CGFloat scaleSize = 0.2f;
UIImage *smallImage = [UIImage imageWithCGImage:image.CGImage
                      scale:scaleSize
                      orientation:image.imageOrientation];



回答2:


Apple's docs state:

The quality of the resulting JPEG image, expressed as a value from 0.0 to 1.0. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).

And since the compression quality is a CGFloat, it supports decimal places beyond the tenths place. That being said, try:

NSData *imageData = UIImageJPEGRepresentation(image, 0.032);



回答3:


First resize the image with below method:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

call this by:

UIImage *newImage=yourImage;
CGSize size=CGSizeMake(newImage.size.width/8,newImage.size.height/8);
newImage=[self resizeImage:newImage newSize:size];

And finally compressed your image as required:

NSData *imageData = UIImageJPEGRepresentation(newImage, 0.5);
NSLog(@"Size of image = %lu KB",(imageData.length/1024));


来源:https://stackoverflow.com/questions/14085055/reduce-camera-photo-library-image-file-size-for-less-than-100-kb-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!