如何在iPhone上更改图像的颜色并快速合成它们?
我有多个图像(UIImage的NSData*)。原因是,我无法在内存中保存太多UIImage。如何在iPhone上更改图像的颜色并快速合成它们?,iphone,uiimage,layer,Iphone,Uiimage,Layer,我有多个图像(UIImage的NSData*)。原因是,我无法在内存中保存太多UIImage。 我想更改它们的颜色并快速合并它们,而不需要一次创建太多的UIImage。 我对2d绘图完全是新手(只是复制和粘贴其他人的代码来完成我需要的工作),而且很难弄清楚怎么做。 我怀疑石英中的“层”概念可以加速我的工作 下面是我用来更改颜色和组合图像的代码。 请注意,我正在为多个图像的每个步骤(更改颜色、合并图像)创建UIImage,但速度很慢 - (UIImage*) changeColor: (UICol
我想更改它们的颜色并快速合并它们,而不需要一次创建太多的UIImage。
我对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;
}
+(UIImage*)组合图像向量:(const std::vector&)图像向量
{
if(imageVector.size()==0)
返回零;
CGSize size=(imageVector.at(0)).size;
CGRect rect=CGRectMake(0,0,size.width,size.height);
UIGraphicsBeginImageContext(大小);
for(无符号整数i=0;i