开发者

How can I change the color of images and composite them quickly on the iPhone?

I have multiple images( NSData* of UIImage). Reason being, I can't hold too many UIImages in memory.

I want to change their color and merge them fast without creating too many UIImages at once at any time.

I'm total newbie to 2d drawings(just copied and pasted other people's code to do what I need) and having hard time figuring out how to do it.

I suspect "Layer" concept in quartz can speed up what I'm doing.

Here's the code that I use to change color and combine images. Note that I'm creating UIImage for each step(change color, combine image) for multiple images, and it is slow..

- (UIImage*) changeColor: (UIColor*) aColor
{
    if(aColor == nil)
        return self;
    UIGraphicsBeginImageContext(self.size);

    CGRect contextRect;
    contextRect.origin.x = 0.0f;
    contextRect.origin.y 开发者_开发百科= 0.0f;
    contextRect.size = self.size;
    // Retrieve source image and begin image context                                                                                                                                                                                        
    CGSize itemImageSize = self.size;
    CGPoint itemImagePosition;
    itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
    itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) );

    UIGraphicsBeginImageContext(contextRect.size);

    CGContextRef c = UIGraphicsGetCurrentContext();
    // Setup shadow                                                                                                                                                                                                                         
    // Setup transparency layer and clip to mask                                                                                                                                                                                            
    CGContextBeginTransparencyLayer(c, NULL);
    CGContextScaleCTM(c, 1.0, -1.0);
    CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [self CGImage]);
    // Fill and end the transparency layer                                                                                                                                                                                                  

    CGColorRef colorRef = aColor.CGColor;
    const CGFloat *components = CGColorGetComponents(colorRef);
    int red = components[0] * 255;
    int green = components[1] * 255;
    int blue = components[2] * 255;

    CGContextSetRGBFillColor(c, red, green, blue, 1);

    contextRect.size.height = -contextRect.size.height;
    contextRect.size.height -= 15;
    CGContextFillRect(c, contextRect);
    CGContextEndTransparencyLayer(c);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

merging images

- (UIImage*) combineImage: (UIImage*) aImage
{
    UIGraphicsBeginImageContext(self.size);
    [self drawInRect: CGRectMake(0, 0, self.size.width, self.size.height)];
    [aImage drawInRect: CGRectMake(0, 0, self.size.width, self.size.height)];

    UIImage* combinedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return combinedImage;
}

// draw multiple images at once to speed up a bit. I control the size of the vector to lower the memory requirement

+ (UIImage*) combineImageVector: (const std::vector<UIImage*>&) imageVector
{
    if(imageVector.size() == 0 )
        return nil;

    CGSize size = (imageVector.at(0)).size;
    CGRect rect = CGRectMake(0,0, size.width, size.height);
    UIGraphicsBeginImageContext(size);

    for(unsigned int i=0; i < imageVector.size(); ++i)
    {
        UIImage* aImage = imageVector.at(i);
        [aImage drawInRect: rect];
    }

    UIImage* combinedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return combinedImage;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜