1. 在ios上可以使用 api uiimagejpegrepresentation 对照片数据进行jpeg压缩;
我们知道ios其实是mac os 的移植,那么mac上肯定也有相应的jpeg压缩方法;
在mac上了,找了nsimage的api没有发现直接的jpeg压缩方法;
但是有nsbitmapimagerep,下面来测试一下,ios和mac上的jpeg压缩是否一致;
2. 首先用ios 来压缩一张照片
uiimage *timg = [uiimage imagewithcontentsoffile:@"/users/cc/desktop/testios/img_0420.png"]; for (int i = 0; i <10; i++) { nsdata *cd = uiimagejpegrepresentation(timg, (i+1)/10.0f); [cd writetofile:[nsstring stringwithformat:@"/users/cc/desktop/testios/com%.1f.jpeg",(i+1)/10.0f] atomically:yes]; }
得到结果:(压缩比0.1~1.0)
3. mac api对照片进行jpeg压缩
// 参数校验 if (argc!=4) { printf("参数错误,请检测!n"); printf("本程序主要是对图片进行jpeg压缩n"); printf("示例:./jpegcompress /xxpath/imgfile /xxpath/out.jpeg 0.4 n"); printf("参数一:要压缩的图片;参数二:输出路径;参数三:压缩比0.1~1.0之间n"); return -1000; } nsstring *inpath = [nsstring stringwithcstring:argv[1] encoding:nsutf8stringencoding]; nsstring *outpath = [nsstring stringwithcstring:argv[2] encoding:nsutf8stringencoding]; float compress = [[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]; int ret = [imgdt writetofile:outpath atomically:yes]; if (ret>0) { printf("in: %snout: %sncompress: %snsuccessn",argv[1],argv[2],argv[3]); }else { printf("failure!n"); } return ret;
得到结果:压缩比(0.1~1.0)
4. 通过上面的结果,可以看出,同样的压缩比,压缩出来的照片大小是一样的;
但是我在比较上面相同大小文件的md5时发现是不一样的;
所以理论上mac和ios上的jpeg压缩是一致的,但并不是完全一致!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!