如何将 UIEdgeInsets
(或其某些变体)应用于 CGImage
?这是我最终要实现的目标,但是 CGImage
在转换为 CGImage
之前无法识别应用于 UIImage
的底部插图>.
var image = someUIImage
image = image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: (image.size.height / 2), right: 0))
let imageCG: CGImage = image.cgImage!
layer.contents = imageCG
而且我无法直接将插图应用于 CGImage
(如此处所示)。
let image = someUIImage.cgImage!
image. // can't apply any inset here
layer.contents = image
解决方法?
最佳答案
您不能将 UIEdgeInsets
应用于 CGImage
,因为 CGImage
没有 alignmentRectInsets
属性. UIImage
存储了一些 CGImage
没有的属性,包括 alignmentRectInsets
。
请注意,alignmentRectInsets
属性的存在是为了影响自动布局如何构成包含图像的 UIImageView
。 UIImageView
根据其 UIImage
的 alignmentRectInsets
设置自己的 alignmentRectInsets
。自动布局然后在计算 UIImageView
的框架时使用对齐矩形。
自动布局不适用于图层,因此 CALayer
不需要 alignmentRectInsets
属性。
换句话说,您的要求没有任何意义。
如果您修改您的问题以解释您想要的视觉效果,我可能会就如何获得它提供建议。
关于ios - 如何将 UIEdgeInsets 应用于 CGImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46201806/