更改3D变换后如何合并两个图像?

时间:2012-12-20 06:24:25

标签: iphone ios uiimage

  

可能重复:
  How to merge the perspective image(3D Transform)?

如何在更改3D变换后合并两个图像?我正在使用两个图像

(i)背景图像是正常图像。

(ii)前景图像改变3D变换。

现在我合并了两张图片。但是没有合并图像,我使用下面的代码。此代码在没有3D变换的情况下工作正常。

UIGraphicsBeginImageContext(backgroundImageView.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[imageView.layer renderInContext:ctx];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
return newImage;

我搜索了很多,但3D转换后的图像不支持该图层。

如何获得此功能。我的应用程序上真的必须这个功能。任何想法,源代码,建议,建议,随时欢迎。请帮我。 enter image description here

1 个答案:

答案 0 :(得分: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;
}