Detailed IOS image compression processing _ios

Source: Internet
Author: User
Tags scale image

Objective

1, the concept of accurate image compression:

"Pressure" refers to the size of the file is smaller, but the number of pixels unchanged, the long width of the same size, then the quality may fall.

"Shrink" refers to the size of the file is smaller, that is, the number of pixels reduced, and the length of the size of small, file size will also be reduced.

2, the picture pressure processing

For the "pressure" function, we can use UIImageJPEGRepresentation or UIImagePNGRepresentation method to implement,

such as code:

Picture pressure
-(void) _imagecompression{
  uiimage *image = [uiimage imagenamed:@ "HD"];
  The first parameter is the picture object, the second parameter is the coefficient of the pressure, the value range is 0~1.
  NSData * ImageData = uiimagejpegrepresentation (image, 0.7);
  UIImage * NewImage = [uiimage imagewithdata:imagedata];
}

2.1 about PNG and JPEG format compression

UIImageJPEGRepresentationThe function requires two parameters: a picture reference and a compression factor that UIImagePNGRepresentation only requires a picture reference as an argument.

UIImagePNGRepresentation(UIImage *image)is much UIImageJPEGRepresentation(UIImage* image, 1.0) larger than the amount of data returned.

The same picture, using UIImagePNGRepresentation(image) the amount of data returned is 200K, and UIImageJPEGRepresentation(image, 1.0) the amount of data returned is only 150K, less than 50K.

If the definition of the picture is not very high, it is recommended to use UIImageJPEGRepresentation , you can significantly reduce the amount of picture data. For example, the picture just taken, the data returned by the call to UIImageJPEGRepresentation(image, 1.0) read the size of 140K, but change the compression coefficient of 0.5 and read data, the size of the returned data is 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 that the first use UIImageJPEGRepresentation, and according to their actual use of the scene, set the compression coefficient, further reduce the size of the picture data.

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

3, picture "shrink" processing

With the [image drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)] ability to "shrink" the image.

/** * Picture compressed to a specified size * @param targetsize the size of the target picture * @param sourceimage source picture * @return Target Picture * *-(uiimage*) Imagebyscalingan
Dcroppingforsize: (cgsize) targetsize withsourceimage: (uiimage *) sourceimage {uiimage *newimage = nil;
Cgsize imagesize = sourceimage.size;
CGFloat width = imagesize.width;
CGFloat height = imagesize.height;
CGFloat targetwidth = targetsize.width;
CGFloat targetheight = targetsize.height;
CGFloat scalefactor = 0.0;
CGFloat scaledwidth = targetwidth;
CGFloat scaledheight = targetheight;
Cgpoint thumbnailpoint = Cgpointmake (0.0,0.0);
  if (Cgsizeequaltosize (imagesize, targetsize) = NO) {cgfloat widthfactor = targetwidth/width;
  CGFloat heightfactor = targetheight/height; if (Widthfactor > Heightfactor) scalefactor = Widthfactor; Scale to fit height else scalefactor = heightfactor;
  Scale to fit width scaledwidth= width * scalefactor;
  Scaledheight = height * scalefactor; Center the image if (Widthfactor > Heightfactor) {thUmbnailpoint.y = (targetheight-scaledheight) * 0.5;
  else if (Widthfactor < heightfactor) {thumbnailpoint.x = (targetwidth-scaledwidth) * 0.5; } uigraphicsbeginimagecontext (Targetsize);
This is crop cgrect thumbnailrect = Cgrectzero;
Thumbnailrect.origin = Thumbnailpoint;
Thumbnailrect.size.width= Scaledwidth;

ThumbnailRect.size.height = Scaledheight;
[Sourceimage Drawinrect:thumbnailrect];
NewImage = Uigraphicsgetimagefromcurrentimagecontext ();

if (newimage = = nil) NSLog (@ "Could not scale image");

 Pop the "context" to "to" the default Uigraphicsendimagecontext ();
return newimage; }

This UIImageJPEGRepresentation(image, 0.0), and UIImagePNGRepresentation(image); is 1 functional.
This [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)] is the function of 2.

Summarize

So, these two have to use together to meet the needs, otherwise you blindly use 1, resulting in blurred picture, but the size is still very large.
The above is in iOS compressed image processing detailed introduction and examples, I hope to learn about the development of iOS help.

Related Article

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.