iOS image compression processing

Source: Internet
Author: User

Understanding Concepts

First, we must make clear that the image compression is actually two concepts:

    1. "Pressure" refers to the size of the file is smaller, but the number of pixels unchanged, the length and width of the dimensions are unchanged, then the quality may fall.
    2. "Shrink" refers to the size of the file is smaller, that is, the number of pixels reduced, and the size of the long-width smaller, the file volume will also be reduced.
picture "Press" Processing

For the function of "pressure", we can use UIImageJPEGRepresentation or UIImagePNGRepresentation implement the method, such as:

123 nsdata *imgdata = uiimagejpegrepresentation(image, 0.5) ;

The first parameter is the picture object, the second parameter is the coefficient of pressure, and its value range is 0~1.

The official notes for the UIImageJPEGRepresentation method are:

return image as JPEG. May return nil If image has no cgimageref or invalid bitmap format. Compression is 0 (most): 1 (least)

About PNG and JPEG format compression
    1. UIImageJPEGRepresentationThe function requires two parameters: a reference to a picture and a compression factor, but UIImagePNGRepresentation only a picture reference as a parameter.
    2. UIImagePNGRepresentation(UIImage \*image)is much UIImageJPEGRepresentation(UIImage* image, 1.0) larger than the amount of picture data returned.

In the same picture, the amount of data returned is the size of the volume, and UIImagePNGRepresentation(image) 199K UIImageJPEGRepresentation(image, 1.0) The amount of data returned is only 140K , less than the former 59K .

If the sharpness of the picture is not very high, it is recommended UIImageJPEGRepresentation to use, can greatly reduce the amount of image data. For example, the image you just shot, UIImageJPEGRepresentation(image, 1.0) when you read the data by calling, returns the size of the data, 140K but when you change the compression factor to 0.5 read the data again, The size of the returned data only 11K , greatly compressed the image of the amount of data, and there is no difference in clarity, the quality of the picture is not significantly reduced. Therefore, when reading the image data content, it is recommended to use first UIImageJPEGRepresentation , and according to their actual use of the scene, set the compression coefficient, further reduce the size of the image data.

Tip: The compression coefficient should not be too low, usually 0.3~0.7, too small may appear black edge and so on.

Let's take a look at UIImageJPEGRepresentation the data tables I've collected:

The author has counted the iphone full screen image and the size of the original image before and after compression, and we need to decide which compression factor to choose according to the sharpness of the image after compression on the PC.

picture "pinch" processing

With the [sourceImage drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)] ability to "shrink" the picture. The following is the size of the picture I shrink api :

12345678910111213141516171819202122232425262728 /*!* @author Du Jianshu, 15-12-01 16:12:01 ** Compress image to target size ** @param sourceimage source image* @param targetwidth the width of the final size of the picture ** @return Return to the image with the width and height of the source image compressed to the target width and height */- (UIImage *)compressimage:(UIImage *) Sourceimage totargetwidth:(cgfloat)targetwidth { cgsize imageSize = sourceimage. Size;    cgfloat width = imageSize. width; cgfloat height = imageSize. Height;    cgfloat targetheight = (targetwidth / width) * height;     uigraphicsbeginimagecontext ( cgsizemake (targetwidth , targetheight) Span class= "Crayon-sy") [sourceimage drawinrect: CGRectMake(0, 0, targetwidth, targetheight)];    UIImage *newimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext();    return newimage; }

We only "pressure" the picture and not shrink, sometimes it is not up to our needs. Therefore, appropriate to the picture "shrink" a size, can meet our needs.

iOS image compression processing

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.