Ios 彩色图像将像素化
我无法理解以下方法返回可见像素化图像的原因。我已经仔细检查了图像的大小,没有问题。更重要的是,在不着色的情况下,图像边缘是平滑的,并且缺少像素化 基于ios7imageview的tintColor属性的图像着色方法工作得很好,但是我很想找出以下代码的错误,因为它似乎适用于除我之外的所有人。谢谢Ios 彩色图像将像素化,ios,uiimageview,uiimage,cgcontext,Ios,Uiimageview,Uiimage,Cgcontext,我无法理解以下方法返回可见像素化图像的原因。我已经仔细检查了图像的大小,没有问题。更重要的是,在不着色的情况下,图像边缘是平滑的,并且缺少像素化 基于ios7imageview的tintColor属性的图像着色方法工作得很好,但是我很想找出以下代码的错误,因为它似乎适用于除我之外的所有人。谢谢 - (UIImage *)imageTintedWithColor:(UIColor *)color { if (color) { UIImage *img = self; // The meth
- (UIImage *)imageTintedWithColor:(UIColor *)color
{
if (color) {
UIImage *img = self; // The method is a part of UIImage category, hence the "self"
UIGraphicsBeginImageContext(img.size);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
// set the fill color
[color setFill];
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeColorBurn);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, rect, img.CGImage);
// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return the color-burned image
return coloredImg;
}
return self;
}
更改此行:
UIGraphicsBeginImageContext(img.size);
致:
如果您的图像永远不会有透明度,请将
NO
更改为YES
您完全正确!来自文档:UIGraphicsBeginImageContext()-此函数相当于调用UIGraphicsBeginImageContextWithOptions函数,不透明参数设置为NO,比例因子为1.0
。所以差异是“比例因子”@rmaddy,你能解释一下比例因子0和1之间的区别吗?0
的值意味着根据设备确定是使用1
还是2
。视网膜设备将导致使用2
,而非视网膜设备将导致使用1
。
UIGraphicsBeginImageContextWithOptions(img.size, NO, 0);