当前位置: 移动技术网 > 移动技术>移动开发>IOS > 详解IOS图片压缩处理

详解IOS图片压缩处理

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

 1、确图片的压缩的概念:

“压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降。

“缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体积同样会减小。

 

 2、图片压的处理

对于“压”的功能,我们可以使用uiimagejpegrepresentationuiimagepngrepresentation方法实现,

如代码:

//图片压
- (void)_imagecompression{
  uiimage *image = [uiimage imagenamed:@"hd"];
  //第一个参数是图片对象,第二个参数是压的系数,其值范围为0~1。
  nsdata * imagedata = uiimagejpegrepresentation(image, 0.7);
  uiimage * newimage = [uiimage imagewithdata:imagedata];
}

2.1关于png和jpeg格式压缩

uiimagejpegrepresentation函数需要两个参数:图片的引用和压缩系数而uiimagepngrepresentation只需要图片引用作为参数.

uiimagepngrepresentation(uiimage *image)要比uiimagejpegrepresentation(uiimage* image, 1.0)返回的图片数据量大很多.

同样的一张照片, 使用uiimagepngrepresentation(image)返回的数据量大小为200k,而 uiimagejpegrepresentation(image, 1.0)返回的数据量大小只为150k,比前者少了50k.

如果对图片的清晰度要求不是极高,建议使用uiimagejpegrepresentation,可以大幅度降低图片数据量.比如,刚才拍摄的图片,通过调用uiimagejpegrepresentation(image, 1.0)读取数据时,返回的数据大小为140k,但更改压缩系数为0.5再读取数据时,返回的数据大小只有11k,大大压缩了图片的数据量,而且清晰度并没有相差多少,图片的质量并没有明显的降低。因此,在读取图片数据内容时,建议优先使用uiimagejpegrepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。

提示:压缩系数不宜太低,通常是0.3~0.7,过小则可能会出现黑边等。

3、图片“缩”处理

通过[image drawinrect:cgrectmake(0, 0, targetwidth, targetheight)]可以进行图片“缩”的功能。

 

/**
 * 图片压缩到指定大小
 * @param targetsize 目标图片的大小
 * @param sourceimage 源图片
 * @return 目标图片
 */
 - (uiimage*)imagebyscalingandcroppingforsize:(cgsize)targetsize withsourceimage:(uiimage *)sourceimage
{
uiimage *newimage = nil;
cgsize imagesize = sourceimage.size;
cgfloat width = imagesize.width;
cgfloat height = imagesize.height;
cgfloat targetwidth = targetsize.width;
cgfloat targetheight = targetsize.height;
cgfloat scalefactor = 0.0;
cgfloat scaledwidth = targetwidth;
cgfloat scaledheight = targetheight;
cgpoint thumbnailpoint = cgpointmake(0.0,0.0);
if (cgsizeequaltosize(imagesize, targetsize) == no)
{
  cgfloat widthfactor = targetwidth / width;
  cgfloat heightfactor = targetheight / height;
  if (widthfactor > heightfactor)
    scalefactor = widthfactor; // scale to fit height
  else
    scalefactor = heightfactor; // scale to fit width
  scaledwidth= width * scalefactor;
  scaledheight = height * scalefactor;
  // center the image
  if (widthfactor > heightfactor)
  {
    thumbnailpoint.y = (targetheight - scaledheight) * 0.5;
  }
  else if (widthfactor < heightfactor)
  {
    thumbnailpoint.x = (targetwidth - scaledwidth) * 0.5;
  }
}
uigraphicsbeginimagecontext(targetsize); // this will crop
cgrect thumbnailrect = cgrectzero;
thumbnailrect.origin = thumbnailpoint;
thumbnailrect.size.width= scaledwidth;
thumbnailrect.size.height = scaledheight;

[sourceimage drawinrect:thumbnailrect];
newimage = uigraphicsgetimagefromcurrentimagecontext();
if(newimage == nil)
  nslog(@"could not scale image");

//pop the context to get back to the default
uigraphicsendimagecontext();

 return newimage;
}

这个uiimagejpegrepresentation(image, 0.0),uiimagepngrepresentation(image); 是1的功能。
这个 [sourceimage drawinrect:cgrectmake(0,0,targetwidth, targetheight)] 是2的功能。

总结

所以,这俩得结合使用来满足需求,不然你一味的用1,导致,图片模糊的不行,但是尺寸还是很大。
以上就是在ios中压缩图片处理的详细介绍及实例,希望对大家学习ios开发有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网