我感觉这不是一件容易的事,但我需要将 UIImageView 与位于其上方的另一个 UIImage View 组合或展平。例如:我有两个 UIImageView。其中之一具有草地的 UIImage(1200 x 1200 像素)。另一个是篮球的 UIImage(128 x 128 像素),它位于草 map 像上方,使篮球看起来位于草地上。我希望能够将叠加的 UIImageViews 作为单个图像文件保存到我的相册中,这意味着我需要以某种方式组合这两个图像。这将如何实现? (注意:截取屏幕截图(320 x 480 像素)不是一个可接受的解决方案,因为我希望保留 1200 x 1600 像素的大小。
问题:
如何将多个 UIImageView 合并为一个并保存生成的图像,同时保留大小/分辨率。
最佳答案
为什么不将原始 UIImage 绘制到彼此之上的背景缓冲区中,然后将组合图像写入文件?下面是如何将两个图像绘制到同一缓冲区的示例:
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);
关于iphone - 如何将 'Flatten' 多个 UIImageView 合并为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1253386/