如何更改图像的颜色并在iPhone上快速合成?

时间:2011-01-13 05:01:05

标签: iphone uiimage layer

我有多个图像(UIImage的NSData *)。原因是,我不能在记忆中容纳太多的UIImages 我想改变它们的颜色并快速合并它们,而不会在任何时候立刻创建太多的UIImages 我是2d图纸的新手(只是复制和粘贴其他人的代码来做我需要的)并且很难弄清楚如何去做。
我怀疑石英中的“层”概念可以加速我正在做的事情。

这是我用来改变颜色和组合图像的代码。 请注意,我正在为多个图像的每个步骤(更改颜色,合并图像)创建UIImage,而且速度很慢。

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

合并图片

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

//一次绘制多个图像以加快速度。我控制向量的大小以降低内存需求

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

1 个答案:

答案 0 :(得分:0)

我使用了此页http://discussions.info.apple.com/message.jspa?messageID=8103870中的代码。它对我有用(对于我的图像)。您可以根据需要尝试修改。