Introduction:
pressure: refers to the size of the file smaller, but the number of pixels unchanged, the length and width of the size of the same, then the quality may fall.
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.
Application:
In the actual development, we often to the image processing, to meet the development needs, the following three kinds of image compression processing:
1. Compress image quality (image volume is reduced, pixels are unchanged)
Two simple ways to read picture data:
(1). The UIImageJPEGRepresentation function requires two parameters: the reference to the picture and the compression factor, and the compression volume does not vary with the compression factor ratio.
(2). Uiimagepngrepresentation only requires a picture reference as a parameter.
Attention:
Uiimagepngrepresentation (image) is much larger than the amount of image data returned by uiimagejpegrepresentation (image, 1.0)
It is recommended to use UIImageJPEGRepresentation first, and set the compression factor according to your actual usage scene, further reduce the size of image data.
+(UIImage *)reduceImage:(UIImage *)image percent:(float)percent{ NSData *imageData = UIImageJPEGRepresentation(image, percent); UIImage *newImage = [UIImage imageWithData:imageData]; return newImage;}
2. Compress the image to the specified size
+ (UIImage *) scaleImage:(UIImage *) image withNewSize:(CGSize) newSize{ UIGraphicsBeginImageContext(newSize); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage;}
3. Compress to a specified size, etc. proportional compression
-(UIImage *) Imagecompressforsize: (UIImage *) Sourceimage targetsize: (Cgsize) size{UIImage *newimage =NilCgsize imageSize = sourceimage. size;CGFloat width = imageSize. width;CGFloat height = imageSize. Height;CGFloat targetwidth = size. width;CGFloat targetheight = size. Height;CGFloat scalefactor =0.0;CGFloat scaledwidth = targetwidth;CGFloat scaledheight = targetheight;Cgpoint Thumbnailpoint =Cgpointmake (0.0,0.0);IfCgsizeequaltosize (imageSize, size) = =NO) {CGFloat widthfactor = targetwidth/width;CGFloat heightfactor = targetheight/height;if (Widthfactor > Heightfactor) {scalefactor = Widthfactor;}else{scalefactor = heightfactor;} scaledwidth = width * scalefactor; Scaledheight = height * scalefactor;if (Widthfactor > Heightfactor) {thumbnailpoint. y = (targetheight-scaledheight) *0.5; }Elseif (Widthfactor < heightfactor) {Thumbnailpoint. x = (targetwidth-scaledwidth) *0.5; } }Uigraphicsbeginimagecontext (size);CGRect Thumbnailrect =Cgrectzero; Thumbnailrect. origin = Thumbnailpoint; Thumbnailrect. Size. width = scaledwidth; Thumbnailrect. Size. height = scaledheight; [Sourceimage Drawinrect:thumbnailrect]; NewImage =Uigraphicsgetimagefromcurrentimagecontext ();if (NewImage = =Nil) {NSLog (@ "scale image fail"); }Uigraphicsendimagecontext ();return newimage;} -(UIImage *) Imagecompressforwidth: (UIImage *) Sourceimage targetwidth: (CGFloat) definewidth{UIImage *newimage =NilCgsize imageSize = sourceimage. size;CGFloat width = imageSize. width;CGFloat height = imageSize. Height;CGFloat targetwidth = definewidth;CGFloat targetheight = height/(width/targetwidth);Cgsize size =Cgsizemake (Targetwidth, targetheight);CGFloat scalefactor =0.0;CGFloat scaledwidth = targetwidth;CGFloat scaledheight = targetheight;Cgpoint Thumbnailpoint =Cgpointmake (0.0,0.0);IfCgsizeequaltosize (imageSize, size) = =NO) {CGFloat widthfactor = targetwidth/width;CGFloat heightfactor = targetheight/height;if (Widthfactor > Heightfactor) {scalefactor = Widthfactor;}else{scalefactor = heightfactor;} scaledwidth = width * scalefactor; Scaledheight = height * scalefactor;if (Widthfactor > Heightfactor) {thumbnailpoint. y = (targetheight-scaledheight) *0.5; }Elseif (Widthfactor < heightfactor) {Thumbnailpoint. x = (targetwidth-scaledwidth) *0.5;} } uigraphicsbeginimagecontext (size); cgrect thumbnailrect = cgrectzero; thumbnailrect. origin = Thumbnailpoint; Thumbnailrect. Size. width = scaledwidth; Thumbnailrect. Size. height = scaledheight; [Sourceimage Drawinrect:thumbnailrect]; NewImage = Uigraphicsgetimagefromcurrentimagecontext (); if (newimage = = nil) { NSLog (@ "scale image fail");} Uigraphicsendimagecontext (); return newimage;}
iOS Development Quest-Image compression processing