Rex*_*ids 7 iphone core-graphics pixel flatten uiimage
我有一种感觉,这不是一件容易的事,但我需要将UIImageView与位于其上方的另一个UIImage视图结合或展平.例如:我有两个UIImageViews.其中一个有草地的UIImage(1200 x 1200像素).另一个是篮球的UIImage(128 x 128像素),它位于草地的图像上方,使篮球看起来像是在草地上.我希望能够将叠加的UIImageViews作为单个图像文件保存到我的相册中,这意味着我需要以某种方式组合这两个图像.这将如何实现?(注意:截取屏幕截图(320 x 480像素)不是一个可接受的解决方案,因为我希望保留1200 x 1600像素的大小.
问题:
如何在保留大小/分辨率的同时将多个UIImageViews拼接成一个并保存生成的图像.
为什么不直接将原始UIImages绘制到背景缓冲区中,然后将组合图像写入文件?下面是一个如何将两个图像绘制到同一缓冲区的示例:
CGImageRef bgimage = [bguiimage CGImage];
width = CGImageGetWidth(bgimage);
height = CGImageGetHeight(bgimage);
// Create a temporary texture data buffer
GLUbyte* data = (GLubyte *) malloc(width * height * 4);
assert(data);
// Draw image to buffer
CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, width * 4, CGImageGetColorSpace(image), kCGImageAlphaPremultipliedLast);
assert(ctx);
// Flip image upside-down because OpenGL coordinates differ
CGContextTranslateCTM(ctx, 0, height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextDrawImage(ctx, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), bgimage);
CGImageRef ballimage = [balluiimage CGImage];
bwidth = CGImageGetWidth(ballimage);
bheight = CGImageGetHeight(ballimage);
float x = (width - bwidth) / 2.0;
float y = (height - bheight) / 2.0;
CGContextDrawImage(ctx, CGRectMake(x, y, (CGFloat)bwidth, (CGFloat)bheight), ballimage);
CGContextRelease(ctx);
Run Code Online (Sandbox Code Playgroud)
这可以获取任何视图并从中生成 UIImage。任何视图及其子视图都将被“展平”为可以显示或保存到磁盘的 UIImage。
- (UIImage*)imageFromView{
UIImage *image;
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Run Code Online (Sandbox Code Playgroud)