iPhone上如何将两个来自不同imageView的图片合并成一张单独的图片?

3
我想将两个来自不同ImageView的图像添加在一起,以使它们具有3D效果,使得两个图像重叠在一起,当用户查看图像时,它显示两个图像,当用户从侧面移动iPhone时,只显示一个图像(仅显示一个图像,另一个图像消失),就像全息图一样。
请帮帮我,这对我非常重要。 谢谢......!!
3个回答

3

如果您想像全息图一样展示图片并仅显示一张,可能的方法如下:

  • 最初将两个图像作为子视图添加到同一个矩形中,并将两个图像的透明度(alpha)设置为0.5。
  • 当用户移动手机时,通过将所需图像的alpha设置为1.0并将该子视图置于前面来显示所需的图像。

这可能会有所帮助。


谢谢您的建议,非常有帮助。我能保存这张图片吗?因为这两张图片在不同的图像视图上。如果可以,请告诉我如何操作? - Surjeet Singh
你们是否有保存重叠图像的要求?你能具体说明一下你们的需求吗? - Amit
要保存重叠的图像,您可以对视图进行截屏。您是否将这些图像视图添加为视图的子视图?如果是,则对视图进行截屏。您可以搜索代码以对视图进行截屏并保存为图像。 - Amit

2
尝试使用此代码合并两个图像。
UIGraphicsBeginImageContext(firstImage.size);
[firstImage drawAtPoint:CGPointMake(0,0)];
[secondImage drawAtPoint:CGPointMake(0,0)];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

1
你可以使用这段代码。
+ (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second
{
 // get size of the first image
 CGImageRef firstImageRef = first.CGImage;
 CGFloat firstWidth = CGImageGetWidth(firstImageRef);
 CGFloat firstHeight = CGImageGetHeight(firstImageRef);

 // get size of the second image
 CGImageRef secondImageRef = second.CGImage;
 CGFloat secondWidth = CGImageGetWidth(secondImageRef);
 CGFloat secondHeight = CGImageGetHeight(secondImageRef);

 // build merged size
 CGSize mergedSize = CGSizeMake(MAX(firstWidth, secondWidth), MAX(firstHeight, secondHeight));

 // capture image context ref
 UIGraphicsBeginImageContext(mergedSize);

 //Draw images onto the context
 [first drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
 [second drawInRect:CGRectMake(0, 0, secondWidth, secondHeight)]; 

 // assign context to new UIImage
 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

 // end context
 UIGraphicsEndImageContext();

 return newImage;
}

尝试这个链接

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接