The photos taken by the iphone are generally sized in 1-10m, and when we upload photos, it is not possible to upload such a large image to the server, generally we will compress the photos.
A common practice is to use this function to compress the image uiimagejpegrepresentation (UIImage * __nonnull image, CGFloat compressionquality); However, this function has a critical value, can not be unlimited compression of the picture, generally to when the compression ratio passed in 0.1 has reached the threshold. At this point, if I want to compress the 5M image to 50K is not feasible.
If the graph is too large, we require that the compression ratio is too large to use the above method to compress, it is necessary to first cut the picture, cut and then use this function compression, so as to achieve the desired size. In order not to distort the picture, crop it by the aspect ratio of the original. The code of classification that can be written as UIImage is as follows:
1-(uiimage*) Imagebyscalingandcroppingforsize: (cgsize) targetsize2 {3UIImage *sourceimage =Self ;4UIImage *newimage =Nil;5Cgsize imageSize =sourceimage.size;6CGFloat width =ImageSize.Width;7CGFloat height =Imagesize.height;8CGFloat targetwidth =Targetsize.width;9CGFloat targetheight =Targetsize.height;TenCGFloat scalefactor =0.0; OneCGFloat scaledwidth =Targetwidth; ACGFloat scaledheight =Targetheight; -Cgpoint Thumbnailpoint = Cgpointmake (0.0,0.0); - the if(Cgsizeequaltosize (imageSize, targetsize) = =NO) - { -CGFloat widthfactor = targetwidth/width; -CGFloat heightfactor = targetheight/height; + - if(Widthfactor >heightfactor) +Scalefactor = Widthfactor;//Scale to fit height A Else atScalefactor = Heightfactor;//Scale to fit width -Scaledwidth= Width *Scalefactor; -Scaledheight = Height *Scalefactor; - - //Center the Image - if(Widthfactor >heightfactor) in { -Thumbnailpoint.y = (targetheight-scaledheight) *0.5; to } + Else if(Widthfactor <heightfactor) - { theThumbnailpoint.x = (targetwidth-scaledwidth) *0.5; * } $ }Panax Notoginseng -Uigraphicsbeginimagecontext (targetsize);//This would crop the +CGRect Thumbnailrect =Cgrectzero; AThumbnailrect.origin =Thumbnailpoint; theThumbnailrect.size.width=Scaledwidth; +ThumbnailRect.size.height =Scaledheight; - $ [Sourceimage Drawinrect:thumbnailrect]; $ -NewImage =Uigraphicsgetimagefromcurrentimagecontext (); - if(NewImage = =Nil) theNSLog (@"could not scale image"); - Wuyi //Pop the context to get back to the default the Uigraphicsendimagecontext (); - returnNewImage; Wu}
Then to the System tool class to write a compressed image of the class method, according to different sizes of the picture with different size of the cutting ratio and compression ratio, the compressed image to ensure that the size of about 50K.
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{ +cout =0.9; -Imgcout = -/length; the } * $ Panax Notoginseng //crop by crop scale -UIImage *compressimage = [img Imagebyscalingandcroppingforsize:cgsizemake (Img.size.width * cout, Img.size.height *cout)]; the + A //that compression proportional compression theImageData =uiimagejpegrepresentation (Compressimage, imgcout); + -Length= Imagedata.length/1024x768; $NSLog (@"cropping ratio:%f, compression ratio:%f, Compressed size:%fkb", cout,imgcout,length); $ returnImageData; -}
iOS picture compression