2014-02-21 28 views
0

我有问题了解以下方法的原因,以返回明显像素化的图像。我已经仔细检查了图像的大小,并且没有问题。更重要的是,如果不着色,图像边缘平滑,并且缺乏像素化。颜色有色UIImages获得像素化

基于IOS7 ImageView的tintColor属性设置图像的方法工作正常,但是我想知道下面的代码有什么问题,因为它似乎适用于除我之外的其他人。谢谢!

- (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; 

} 

回答

1

改变这一行:

UIGraphicsBeginImageContext(img.size); 

到:

UIGraphicsBeginImageContextWithOptions(img.size, NO, 0); 

如果您的图片将永远不会有一个透明度,改变NOYES

+0

你是完全正确的!从文档:UIGraphicsBeginImageContext() - 此函数等效于调用UIGraphicsBeginImageContextWithOptions函数,其opaque参数设置为NO并且缩放因子为1.0'。所以差异是“比例因子”。 @rmaddy,你能解释比例因子0和1之间有什么区别吗? – Michael

+1

'0'的值表示根据设备确定是否应使用'1'或'2'的值。视网膜设备将导致使用“2”,而非视网膜设备导致使用“1”。 – rmaddy

+0

非常棒的答案,非常感谢 – Michael