iphone - 如何将 'Flatten' 多个 UIImageView 合并为一个?

标签 iphone uiimage core-graphics pixel flatten

我感觉这不是一件容易的事,但我需要将 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/

相关文章:

iphone - 如何生成适用于所有 iOS 版本的唯一标识符?

iphone - 对于某些国家/地区,iPhone 上的 CTCarrier 返回了错误的 ISO

ios - 从给定的 UIImage 创建有色 UIImage

ios - 如何为 UITextField 制作边框填充?

ios - Objective-C iPhone游戏编译错误

iphone - Objective-C 中不常用的符号

ios - 如何设置图像从base64字符串的宽度和高度转换?

swift - CIImage 的 PNG/JPEG 表示形式始终返回 nil

ios - 混合模式 kCGBlendModeMultiply 没有正确地将图像与颜色相乘

ios - 检测图像 ios 上的黑线