相关疑难解决方法(0)

改变UIImage颜色

我试图改变UIImage的颜色.我的代码:

-(UIImage *)coloredImage:(UIImage *)firstImage withColor:(UIColor *)color {
    UIGraphicsBeginImageContext(firstImage.size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    [color setFill];

    CGContextTranslateCTM(context, 0, firstImage.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGRect rect = CGRectMake(0, 0, firstImage.size.width, firstImage.size.height);
    CGContextDrawImage(context, rect, firstImage.CGImage);

    CGContextClipToMask(context, rect, firstImage.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathElementMoveToPoint);

    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return coloredImg;
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但获得的图像不是很好:返回图像的边界像素是间歇性的,不像我的第一张图像那么平滑.我该如何解决这个问题?

iphone objective-c uiimage cgcontext ios

87
推荐指数
6
解决办法
8万
查看次数

标签 统计

cgcontext ×1

ios ×1

iphone ×1

objective-c ×1

uiimage ×1