Often encountered in the project, upload pictures of the operation, because the iphone directly photographed pictures are often larger, general 3-4m, if the direct upload does not do processing will waste users a lot of traffic, and there are many scenes do not need high-definition pictures, so in the image before uploading pictures to compress, is very necessary.
Ready-made compression functions uiimagejpegrepresentation (UIImage * __nonnull image, CGFloat compressionquality) are available in the Uikit in 1.OC, But the compression ratio can only be 0.1 to 0.9, if the picture is too large, still can not achieve the effect we want.
2. For large images (10M or more), we can first crop and then compress the image. This method can greatly compress the size of the image. Take my project needs as an example, the demand is: no matter how big pictures, are compressed to about 50KB can be uploaded to the server, and the pixel can not be excessive distortion.
1+ (NSData *) compresswithorgimg: (UIImage *) img2 {3 4NSData *imagedata = UIImageJPEGRepresentation (IMG,1);5 floatLength =imagedata.length;6Length = length/1024x768;7NSLog (@"size before compression:%FKB", length);8 //cropping scale9CGFloat cout =0.5;Ten One //Compression ratio ACGFloat imgcout =0.1; - if(Length >25000){//pictures of 25M or more -cout =0.1; theImgcout =0; -}Else if(Length >10000){//pictures of 10M or more -cout =0.2; -Imgcout =0; +}Else if(Length > the) {//pictures of 5M or more -cout =0.3; +Imgcout =0; A}Else if(Length > the) {//if the original image is greater than 1.5M, change a compression level atcout =0.7; -Imgcout =0.1; -}Else if(Length > +) { -cout =0.8; -Imgcout =0.2; -}Else if(Length > -) { incout =0.8; -Imgcout =0.3; to}Else if(Length > -){//less than 500k without cropping + -ImageData = UIImageJPEGRepresentation (IMG, -/imagedata.length); the floatLength =imagedata.length; *Length = length/1024x768; $NSLog (@"size after compression:%FKB", length);Panax Notoginseng returnImageData; -}Else{ the +ImageData = UIImageJPEGRepresentation (IMG,0.5); A floatLength =imagedata.length; theLength = length/1024x768; +NSLog (@"size after compression:%FKB", length); - returnImageData; $ } $ - - //crop by crop scale theUIImage *compressimage = [img Imagebyscalingandcroppingforsize:cgsizemake (Img.size.width * cout, Img.size.height *cout)]; - Wuyi the //that compression proportional compression -ImageData =uiimagejpegrepresentation (Compressimage, imgcout); Wu -Length= Imagedata.length/1024x768; AboutNSLog (@"cropping ratio:%f, compression ratio:%f, Compressed size:%fkb", cout,imgcout,length); $ returnImageData; -}
1 //cropping2-(uiimage*) Imagebyscalingandcroppingforsize: (cgsize) targetsize3 {4UIImage *sourceimage =Self ;5UIImage *newimage =Nil;6Cgsize imageSize =sourceimage.size;7CGFloat width =ImageSize.Width;8CGFloat height =Imagesize.height;9CGFloat targetwidth =Targetsize.width;TenCGFloat targetheight =Targetsize.height; OneCGFloat scalefactor =0.0; ACGFloat scaledwidth =Targetwidth; -CGFloat scaledheight =Targetheight; -Cgpoint Thumbnailpoint = Cgpointmake (0.0,0.0); the - if(Cgsizeequaltosize (imageSize, targetsize) = =AOF - { -CGFloat widthfactor = targetwidth/width; +CGFloat heightfactor = targetheight/height; - + if(Widthfactor >heightfactor) AScalefactor = Widthfactor;//Scale to fit height at Else -Scalefactor = Heightfactor;//Scale to fit width -Scaledwidth= Width *Scalefactor; -Scaledheight = Height *Scalefactor; - - //Center the Image in if(Widthfactor >heightfactor) - { toThumbnailpoint.y = (targetheight-scaledheight) *0.5; + } - Else if(Widthfactor <heightfactor) the { *Thumbnailpoint.x = (targetwidth-scaledwidth) *0.5; $ }Panax Notoginseng } - theUigraphicsbeginimagecontext (targetsize);//This would crop + ACGRect Thumbnailrect =Cgrectzero; theThumbnailrect.origin =Thumbnailpoint; +Thumbnailrect.size.width=Scaledwidth; -ThumbnailRect.size.height =Scaledheight; $ $ [Sourceimage Drawinrect:thumbnailrect]; - -NewImage =Uigraphicsgetimagefromcurrentimagecontext (); the if(NewImage = =Nil) -NSLog (@"could not scale image");Wuyi the //Pop the context to get back to the default - Uigraphicsendimagecontext (); Wu returnNewImage; -}
The code for different size of the picture, gives a different compression ratio, to ensure that the compressed image size is around 50k. "Cropping" here reduces the width and height of the picture to the specified ratio
iOS image compression