0

我希望将背景(UIImageView)与上层的 UIView(MyView)结合起来,然后对合并后的图像进行截图。我正在使用下面的代码,但背景完全覆盖了我的 UIView ......请问有什么建议吗?

UIImageView *ViewToBeAddedAsBackground = BackgroundImageView;    
ViewToBeAddedAsBackground.frame = CGRectMake(0,0,770,410); 
[MyView addSubview:ViewToBeAddedAsBackground];    

UIGraphicsBeginImageContext(MyView.frame.size);
[MyView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *MyView_IMAGE = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(MyView_IMAGE, nil, nil, nil);
4

1 回答 1

1

发生这种情况是因为您添加ViewToBeAddedAsBackgroundsubViewof yourView,所以它通常会覆盖它,您应该做的是相反,添加yourView为子视图ViewToBeAddedAsBackground

接着

[insertSubview:ViewToBeAddedAsBackground.layer renderInContext:UIGraphicsGetCurrentContext()];

您可以尝试以下方法,具体取决于您当前的视图继承性,它可以工作

[MyView.superview insertSubview:ViewToBeAddedAsBackground belowSubview:MyView];
UIGraphicsBeginImageContext(MyView.frame.size);
[MyView.superview.layer renderInContext:UIGraphicsGetCurrentContext()];
于 2012-06-27T12:20:42.423 回答