我正在使用这种方法进行压缩 UIImage
if (actualHeight > maxHeight || actualWidth > maxWidth)
{
if(imgRatio < maxRatio)
{
//adjust width according to maxHeight
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
}
else if(imgRatio > maxRatio)
{
//adjust height according to maxWidth
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
}
else
{
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();
但是对于某些对象UIImage
,在它们的底部,有一条1像素的白线。关于可能的原因有什么建议吗?非常感谢!
问题可能是您正在使用CGFloats,并且要对其进行乘/除,这将导致非整数坐标。
线
actualWidth = imgRatio * actualWidth;
和
actualHeight = imgRatio * actualHeight;
可能会导致非整数坐标。使用ceilf
或floorf
或roundf
对此进行补救。例如
actualWidth = ceilf(imgRatio * actualWidth);
actualHeight = ceilf(imgRatio * actualHeight);
没有它会发生什么
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
可能实际上是(0,0,24.33, 24.33)
您可能实际上正在绘制此大小的rect,但是图像的高度和宽度必须具有整数个像素,因此Apple可能将rect向上舍入以创建图像上下文。
但是随后他们可能会使用抗锯齿功能对其进行绘制,因此它在视觉上确实看起来像非整数像素。这就是为什么您会遇到白线,并且可能还会导致质量下降。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句