在iPhone中将来自不同imageView的两幅图像合并到一幅图像中

在iPhone中将来自不同imageView的两幅图像合并到一幅图像中,iphone,ios,Iphone,Ios,我想从不同的imageview中添加两个图像,使它们具有3d外观,以便两个图像相互重叠,当用户显示图像时,它会显示两个图像,当用户移动iphone时,它会显示一个图像(仅显示一个图像,其他图像会消失),就像全息图一样 请帮帮我,这对我很重要。 谢谢你 请尝试此代码以合并两个图像 UIGraphicsBeginImageContext(firstImage.size); [firstImage drawAtPoint:CGPointMake(0,0)]; [secondImage drawAtPo

我想从不同的imageview中添加两个图像,使它们具有3d外观,以便两个图像相互重叠,当用户显示图像时,它会显示两个图像,当用户移动iphone时,它会显示一个图像(仅显示一个图像,其他图像会消失),就像全息图一样

请帮帮我,这对我很重要。
谢谢你

请尝试此代码以合并两个图像

UIGraphicsBeginImageContext(firstImage.size);
[firstImage drawAtPoint:CGPointMake(0,0)];
[secondImage drawAtPoint:CGPointMake(0,0)];

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

由于您希望像全息图一样显示图像,然后只显示一个,一种可能的方法是:

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

您可以使用此代码

+ (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;
}

或者试试这个。

谢谢你的建议,它真的很有帮助。由于两个图像位于不同的图像视图上,我可以保存此图像吗??如果是,请告诉我如何保存重叠图像?您可以更具体地说明您的要求。要保存重叠图像,您可以拍摄视图的屏幕截图。是否将这些图像视图作为子视图添加到视图中?如果是,则拍摄视图的屏幕截图。您可以搜索代码以获取视图的屏幕截图并另存为图像。