JPEG compression of photos by iOS mac
1. The image data can be JPEG compressed using API uiimagejpegrepresentation on iOS;
We know that iOS is actually a porting of Mac OS, then there must be a corresponding JPEG compression method on Mac;
On the Mac, find the Nsimage API did not find a direct JPEG compression method;
But there are nsbitmapimagerep, below to test, iOS and Mac JPEG compression is consistent;
2. First compress a picture with iOS
UIImage *timg = [UIImage imagewithcontentsoffile:@ " /users/cc/desktop/testios/img_0420.png " ]; for (int i = 0 ; I <10 ; I++ *cd = UIImageJPEGRepresentation (timg, (I+1 )/10.0f ); [CD writetofile:[nsstring stringWithFormat: @ " /users/cc/desktop/testios/com%.1f.jpeg ", (I+1 )/ 10.0f ] atomically:yes]; }
Results obtained: (compression ratio 0.1~1.0)
3. JPEG compression of photos by MAC API
//parameter Check if(argc!=4) {printf ("parameter error, please detect! \ n"); printf ("This program is mainly for JPEG compression of images \ n"); printf ("example:./jpegcompress/xxpath/imgfile/xxpath/out.jpeg 0.4 \ n"); printf ("parameter one: the picture to compress; parameter two: output path; parameter three: compression ratio 0.1~1.0 \ n"); return- +; } nsstring*inpath = [NSString stringwithcstring:argv[1] encoding:nsutf8stringencoding]; NSString*outpath = [NSString stringwithcstring:argv[2] encoding:nsutf8stringencoding]; floatcompress = [[NSString stringwithcstring:argv[3] encoding:nsutf8stringencoding] floatvalue]; Nsimage*simg =[[Nsimage Alloc]initwithcontentsoffile:inpath]; NSData*IMGDT =[simg tiffrepresentation]; Nsbitmapimagerep*imagerep =[Nsbitmapimagerep IMAGEREPWITHDATA:IMGDT]; Nsdictionary*imageprops =[nsdictionary dictionarywithobject:[nsnumber numberwithfloat:compress] forkey:nsimagecompressionfactor]; IMGDT=[Imagerep Representationusingtype:nsjpegfiletype properties:imageprops]; intRET =[Imgdt Writetofile:outpath Atomically:yes]; if(ret>0) {printf ("In :%s\nout:%s\ncompress:%s\nsuccess\n", argv[1],argv[2],argv[3]); }Else{printf ("failure!\n"); } returnRet
Results obtained: Compression ratio (0.1~1.0)
4. Through the above results, it can be seen that the same compression ratio, the compressed photo size is the same;
But I found it was different when comparing the MD5 of the same size file above;
So in theory, JPEG compression on Mac and iOS is consistent, but not exactly the same!
Test program Download!
JPEG compression of photos by iOS mac