颜色着色的UIImages变得像素化

时间:2014-02-21 00:50:37

标签: ios uiimageview uiimage cgcontext

我无法理解以下方法的原因,返回明显像素化的图像。我仔细检查了图像的大小,很好。更重要的是,没有着色,图像边缘是平滑的,并且缺乏像素化。

基于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 个答案:

答案 0 :(得分:1)

更改此行:

UIGraphicsBeginImageContext(img.size);

为:

UIGraphicsBeginImageContextWithOptions(img.size, NO, 0);

如果您的图片永远不会透明,请将NO更改为YES