如何“压扁”多的UIImageViews成一个?
-
12-09-2019 - |
题
我有一种感觉,这不是一件容易的事,但我需要组合或压平与位于其上的另一UIImage的视图一个UIImageView。例如:我有两个UIImageViews的。他们中的一个具有一片草地(1200 x 1200像素)的一个UIImage。另一种是篮球(128×128像素)的一个UIImage,并且它以这样的方式定位在草地场的图像上面说的篮球似乎是在草地上。我希望能够保存叠加的UIImageViews作为一个图像文件到我的写真集,这意味着我需要将两个图像结合莫名其妙。这将如何实现呢? (注意:获取屏幕截图(320×480像素)将不能作为我希望保留1200×1600像素的尺寸是可接受的解决方案
问题:结果 <强>如何变平多的UIImageViews成一个并保存生成的图像,同时保持尺寸/分辨率。强>
解决方案
此需要的任何视图,这使一个UIImage出来。任何视图和它的子视图将被“扁平化”进入,您可以显示或保存到磁盘上一个UIImage。
- (UIImage*)imageFromView{
UIImage *image;
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
其他提示
为什么你不只是画原来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);
不隶属于 StackOverflow