【问题标题】:Tint UIImage without redrawing在不重绘的情况下为 UIImage 着色
【发布时间】:2012-02-14 16:18:20
【问题描述】:

我正在寻找一种能够改变光源周围球体颜色的能力。现在,我通过使用 Color 创建一个 CGImage 并用 PNG 遮罩它来做到这一点。这可行,但是每当我更改颜色时,我都需要重新绘制整个图像,这真的很慢。

是否可以在不重绘的情况下为图像着色?

感谢您的帮助

这是我目前创建球体的方式:

+ (UIImage *)imageWithColor:(UIColor *)color andImage:(UIImage *)image
{
    // create Image from Color
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,[color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // mask color
    CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(image.CGImage),
                                              CGImageGetHeight(image.CGImage),
                                              CGImageGetBitsPerComponent(image.CGImage),
                                              CGImageGetBitsPerPixel(image.CGImage),
                                              CGImageGetBytesPerRow(image.CGImage),
                                              CGImageGetDataProvider(image.CGImage), NULL, false);


    CGImageRef masked = CGImageCreateWithMask([img CGImage], actualMask);
    CGImageRelease(actualMask);
    UIImage * retImage = [UIImage imageWithCGImage:masked];
    CGImageRelease(masked);
    return retImage;
}

好吧,如果有人知道如何衡量哪个函数调用那么慢,我将不胜感激。我使用 Instruments 执行了测量帧速率的应用程序,在更改颜色的同时,我得到了大约每秒 3 帧,这不是我所期望的.. ;)

【问题讨论】:

    标签: image ios5 uiimage tint


    【解决方案1】:

    您需要使用 Time Profiler 选项运行 Instruments。

    【讨论】:

      猜你喜欢
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多