如何将UIEdgeInsets (或其变体)应用于CGImage?这就是我最终想要实现的目标,但是CGImage在将UIImage转换为CGImage之前无法识别应用于它的底部嵌入。
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解决方法?
发布于 2017-09-14 00:04:56
您不能对CGImage“应用UIEdgeInsets”,因为CGImage没有alignmentRectInsets属性。UIImage存储CGImage不能存储的多个属性,包括alignmentRectInsets。
请注意,alignmentRectInsets属性的存在是为了影响自动布局如何生成包含图像的UIImageView。UIImageView从其alignmentRectInsets的alignmentRectInsets设置自己的UIImage。自动布局然后在计算UIImageView的帧时使用对齐矩形。
自动布局不适用于层,因此CALayer不需要alignmentRectInsets属性。
换句话说,你所要求的没有真正的意义。
如果你修改你的问题,解释你想要什么视觉效果,我也许可以提供如何获得它的建议。
发布于 2017-09-13 23:47:48
尝试这样做:
让resizable = img.resizableImage(withCapInsets: UIEdgeInsets(顶部: 8,左侧: 8,底部: 8,右侧: 8),resizingMode:.stretch)
https://stackoverflow.com/questions/46201806
复制相似问题