【问题标题】:Color tinted UIImages gets pixelated彩色 UIImages 像素化
【发布时间】:2014-02-21 00:50:36
【问题描述】:

我无法理解以下方法的原因,即返回明显像素化的图像。我已经仔细检查了图像的大小,这很好。更重要的是,在不着色的情况下,图像边缘平滑,没有像素化。

基于 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;

}

【问题讨论】:

    标签: ios uiimageview uiimage cgcontext


    【解决方案1】:

    改变这一行:

    UIGraphicsBeginImageContext(img.size);
    

    到:

    UIGraphicsBeginImageContextWithOptions(img.size, NO, 0);
    

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

    【讨论】:

    • 你完全正确!来自文档:UIGraphicsBeginImageContext() - This function is equivalent to calling the UIGraphicsBeginImageContextWithOptions function with the opaque parameter set to NO and a scale factor of 1.0。所以区别是“比例因子”。 @rmaddy,你能解释一下比例因子 0 和 1 之间的区别吗?
    • 0的值是指根据设备来决定是使用1还是2。视网膜设备将导致使用2,而非视网膜设备导致使用1
    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 2013-10-14
    相关资源
    最近更新 更多