【发布时间】:2016-03-01 08:12:24
【问题描述】:
我的应用程序允许用户前后切换UIImageView,然后用户可以捕获该屏幕。这是我将屏幕捕获到UIImage的代码
-(UIImage *)imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 2.0f);
// I even tried view.layer.presentationLayer but still not working
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
我不使用[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES],因为它速度较慢,有时(当视图不可见时)会将屏幕截图绘制为黑色。
但 renderInContext 的问题是在UIImageView ( imageView.layer.zPosition = 0.01;etc ) 之间变化的 Z 位置。在我的 iPhone 屏幕上,当我将值分配给 zPosition 时,它可以正常工作,但在捕获屏幕中它结果是错误的。
无论如何我可以解决这个问题吗?提前致谢
编辑: 这是我在捕获屏幕截图之前尝试做的事情。我使用此代码使一个 ImageView 显示在另一个之前。
-(void)bringOneLevelUp:(UIImageView*)imageView
{
//_imgArray is sorted by Z position order (small to big)
NSUInteger currentObjectIndex = [_imgArray indexOfObject:imageView];
if(currentObjectIndex+1< _imgArray.count){
UIImageView *upperImageView = [_imgArray objectAtIndex:currentObjectIndex+1];
CGFloat currentZIndex = imageView.layer.zPosition;
CGFloat upperZIndex= upperImageView.layer.zPosition;
imageView.layer.zPosition = upperZIndex;
upperImageView.layer.zPosition = currentZIndex;
// swap position in array
[_imgArray exchangeObjectAtIndex:currentObjectIndex withObjectAtIndex:currentObjectIndex+1];
}
}
就像我之前解释的那样,手机屏幕上的这段代码的结果是正确的。 newImageView 在 lastImageView 前面。但是当我通过 renderInContext 捕获屏幕截图时。他们不是。
【问题讨论】:
-
“结果错误”是什么意思?您正在尝试什么 z 位置值?
-
我交换了 UIImageView 的 z 位置。它在手机屏幕上看起来是正确的,但在我尝试捕获它时却不正确。
-
我不太明白你的代码在做什么。为什么你没有在你的问题中显示它?
-
我已经添加了您要求的代码。但这无关紧要。问题是 renderInContext 没有捕获视图层的 zPosition。我尝试使用 layer.presentationLayer。但它仍然无法正常工作
标签: ios objective-c iphone uiimageview calayer