0
绘制背景图片我想提请上有我用下面的代码设置背景图像的UIView:使用CGContextDrawImage
- (void)setBackgroundImageFromData:(NSData *)imageData {
UIImage *image = [UIImage imageWithData:imageData];
int width = image.size.width;
int height = image.size.height;
CGSize size = CGSizeMake(width, height);
CGRect imageRect = CGRectMake(0, 0, width, height);
UIGraphicsBeginImageContext(size);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0, height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawImage(currentContext, imageRect, image.CGImage);
UIGraphicsEndImageContext();
}
初始视图使用来自苹果的GLPaint示例中的代码创建的。对于我的生活,这个背景图像没有显示。我错过了什么?
谢谢!
感谢您的回复。我如何设置上下文?我尝试使用CGContextRestoreGState(currentContext);但没有任何改变。 – 2011-05-10 18:51:11
将上下文设置为什么?上下文只是你画的东西;如果你想创建一个显示CGContext的视图,那么最简单的方法是将其转换为UIImage并使用UIImageView。但是你从一个UIImage开始,所以这有点傻。如果你想把它合并到你自定义的UIView子类中,那么你需要保留它,并在被问及时适当地合成它,通常在drawView:中。 – Tommy 2011-05-10 19:16:46
我最终有两个单独的视图,父视图保存绘画视图,并根据需要将父视图的背景设置为图像。感谢您的建议。 – 2011-05-10 19:52:28