IOS image Compression

Source: Internet
Author: User

Image compression before uploading a picture:

NSData *imagedata = uiimagejpegrepresentation (Self.uploadimage, 0.75);
Image by 0.75 mass compression-"converted to NSData

NSString *file_name = [Timeutil getdatestringwithformat:@ "Yyyy-mm-dd-hh-mm-ss"];
Take the time for the picture name

NSString *upload_name =[nsstring stringwithformat:@ "%@.jpg", file_name];
The name of the uploaded image


[Request Setdata:imagedata withfilename:upload_nameandcontenttype:@ "Image/jpeg" forkey:@ "Upload_file"];

The UIImageJPEGRepresentation function requires two parameters: a reference to the picture and a compression factor.
Uiimagepngrepresentation only requires a picture reference as a parameter.

Comparison found:
Uiimagepngrepresentation (uiimage* image) is much larger than the amount of image data returned by uiimagejpegrepresentation (uiimage* image, 1.0).
For example, a photo that reads the same view as the camera
Uiimagepngrepresentation () The amount of data returned is 199K
The amount of data returned by UIImageJPEGRepresentation (uiimage* image, 1.0) is only 140KB
More than 50 KB less than the former.

If the clarity of the picture is not required, you can also significantly reduce the amount of image data by setting the second parameter of the UIImageJPEGRepresentation function.
For example, a picture just taken, when reading data by calling UIImageJPEGRepresentation (uiimage* image, 1.0), returns the data size 140KB
However, after changing the compression factor, when reading data by calling UIImageJPEGRepresentation (uiimage* image, 0.5), the returned data size is only more than 11KB, which greatly compresses the amount of data in the image.

And from a perspective point of view, the quality of the picture is not significantly reduced. Therefore, when reading the image data content, it is recommended to use uiimagejpegrepresentation, and according to their actual use of the scene, set the compression coefficient, further reduce the size of the image data.

IOS image Compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.