[英]UIImage with resizableimagewithcapinsets is distorted when trying to convert containing view to UIImage using [layer renderInContext:]
我对这个层次结构有一个看法:
-UIView -UIImageView -UILabel
UIImageView 包含一个带有可调整大小/可拉伸图像的 UIImage,使用 resizableimagewithcapinssets 创建。
当我截取屏幕截图时,一切看起来都很好:
http://i.stack.imgur.com/uA6Lg.png
但是当我尝试将视图捕获到 UIImage 时,结果是这样的:
http://i.stack.imgur.com/IzUyS.jpg
如您所见,拉伸图像的底部看起来很有趣。
这是我用来将视图捕获到 UIImage 的代码:
- (UIImage *) imageWithView:(UIView *)view
{
[view setBackgroundColor:[UIColor whiteColor]];
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 2.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[view setBackgroundColor:[UIColor clearColor]];
return img;
}
注意:该应用程序将仅用于@2x 设备。
似乎resizableImageWithCapInsets:
在尝试捕获视图层时存在问题。
恢复到已弃用的stretchableImageWIthLeftCapWidth:topCapHeight:
方法解决了这个问题。 但是恢复到已弃用的方法感觉不对,如果有人可以按原样解决问题,我会很高兴。
使用drawViewHierarchyInRect
替换renderInContext
。 有关更多信息,请参阅如何更快地将视图渲染为图像?
我刚刚遇到了类似的问题(尽管现在是用 Swift 编写的)并且在寻找解决方案时遇到了这个问题。
我发现我的解决方案是确保我使用的图像的中心“可拉伸”部分仅为 1x1 像素。
我认为当要渲染的区域小于使用的原始可调整大小的图像时会出现问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.