I'm looking for an ability change the color of a sphere around a Light source. Right now I do that by creating a CGImage out of a Color and masking it with a PNG. This works, but whenever I change the Color I need to redraw the whole Image which is really slow.

Is there a Possibility to tint an Image without redrawing it?

Thanks for your Help

This is my current Way to create the Sphere:

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

Well if anybody knows howto measure which function call is that slow, I would appreciate the Answer. I executed the App measuring Frame Rate with Instruments and while changing Color I get around 3 Frames per second which isn't what I would expect.. ;)

有帮助吗?

解决方案

You need to run Instruments with the Time Profiler option.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top