JPEG compression of photos by iOS mac

Source: Internet
Author: User

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

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.