相应于滚动视图上下文中的一些UIColor值我想要更改之后要覆盖的几个UIImages的像素rgb值。 至少我有4个我想要叠加的图像,一个静态的背景图像和3层图像,其像素信息我想在重叠之前改变它们。 我正在努力解决的问题是UIImages的颜色不正确。我记得在某个地方我读过有关线程问题的困难。但是我不确定我的代码中是否存在同样的问题:
scrollViewDidEndDecelerating委托方法获取3层图像并将它们传递给为像素进行颜色更改的方法。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
MyViewController *controller = [scrollViewControllers objectAtIndex:currentScrollViewPage];
int color1 = [self getIntFromUIColor:controller.view1.backgroundColor];
int color2 = [self getIntFromUIColor:controller.view2.backgroundColor];
int color3 = [self getIntFromUIColor:controller.view3.backgroundColor];
UIImage *bgImage = [UIImage imageName:@"bg.png"];
//pass the overlay images to the coloring method
//I suppose my problem to be here
UIImage *layer1 = [self convertImage:[UIImage imageName:@"layer1.png"] withColor:color1];
UIImage *layer2 = [self convertImage:[UIImage imageName:@"layer2.png"] withColor:color2];
UIImage *layer3 = [self convertImage:[UIImage imageName:@"layer3.png"] withColor:color3];
CGSize sizeBG = bgImage.size;
CGRect bgBoundingBox = CGRectMake (0, 0, sizeBG.width,sizeBG.height);
CGContextRef myBitmapContext = [self createBitmapContextOfSize:sizeBG];
//here I'd like to overlay the images
CGContextDrawImage(myBitmapContext, bgBoundingBox, bgImage.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer1.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer2.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer3.CGImage);
UIImage *result = [UIImage imageWithCGImage: CGBitmapContextCreateImage (myBitmapContext)];
CGContextRelease (myBitmapContext);
//finally paint the result on the imageView
imageView.image = result;
}
以下方法获取UIImage和颜色:
-(void) convertImage:(UIImage *)src_img withColor:(int)color
{
[src_img retain];
CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDeviceRGB();
/*
* Note we specify 4 bytes per pixel here even though we ignore the
* alpha value; you can't specify 3 bytes per-pixel.
*/
int height = src_img.size.height;
size_t d_bytesPerRow = src_img.size.width * 4;
unsigned int * imgData = (unsigned int*)malloc(src_img.size.height*d_bytesPerRow);
CGContextRef context = CGBitmapContextCreate(imgData, src_img.size.width,
src_img.size.height,
8, d_bytesPerRow,
d_colorSpace,
kCGImageAlphaPremultipliedLast);
UIGraphicsPushContext(context);
// These next two lines 'flip' the drawing so it doesn't appear upside-down.
CGContextTranslateCTM(context, 0.0, src_img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Use UIImage's drawInRect: instead of the CGContextDrawImage function, otherwise you'll have issues when the source image is in portrait orientation.
[src_img drawInRect:CGRectMake(0.0, 0.0, src_img.size.width, src_img.size.height)];
UIGraphicsPopContext();
//swap colors
int red = (color & 0xff0000) >> 16;
int green = (color & 0xff00) >> 8;
int blue = (color & 0xff);
int colorShifted = (blue << 16) + (green << 8) + red;
//in this function the pixels are manipulated
colorChanger((int *)imgData,(src_img.size.height * src_img.size.width),colorShifted);
// After we've processed the raw data, turn it back into a UIImage instance.
CGImageRef new_img = CGBitmapContextCreateImage(context);
UIImage * convertedImage = [[[UIImage alloc] initWithCGImage:
new_img] autorelease];
CGImageRelease(new_img);
CGContextRelease(context);
CGColorSpaceRelease(d_colorSpace);
free(imgData);
[src_img release];
return convertedImage;
}
任何帮助将不胜感激。 changeColor函数运行安静。当我只改变一个单层的颜色时,结果看起来很好。但是同时改变所有3层的颜色,主要是layer2和layer3用color3绘制,layer1用color2绘制。 谢谢你的帮助...
答案 0 :(得分:0)
与此同时,我通过更改绘图顺序来解决问题:
CGContextDrawImage(myBitmapContext, bgBoundingBox, bgImage.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer1.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer2.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer3.CGImage);
到
CGContextDrawImage(myBitmapContext, bgBoundingBox, bgImage.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer3.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer2.CGImage);
CGContextDrawImage(myBitmapContext, bgBoundingBox, layer1.CGImage);
但这根本不适合我,因为当我使用另一个绘图订单时,我没有看到关于绘图顺序和颜色不匹配的连接......