简体   繁体   English

如何将UIEdgeInsets应用于CGImage?

[英]How to apply UIEdgeInsets to CGImage?

How does one apply UIEdgeInsets (or some variation thereof) to a CGImage ? 如何将UIEdgeInsets (或其一些变体)应用于CGImage This is what I'm ultimately trying to achieve but the CGImage is not recognizing the bottom inset applied to the UIImage before it is converted to a CGImage . 这是我最终要实现的目标,但是CGImage在将UIImage转换为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


And I can't apply an inset to the CGImage directly (as seen here). 而且我不能直接将插图应用于CGImage (如此处所示)。

let image = someUIImage.cgImage!
image. // can't apply any inset here
layer.contents = image

Workaround? 解决方法?

You cannot “apply UIEdgeInsets ” to a CGImage because a CGImage doesn't have an alignmentRectInsets property. 你不能“适用UIEdgeInsets ”到CGImage因为CGImage不具有alignmentRectInsets财产。 A UIImage stores several properties that a CGImage does not, including alignmentRectInsets . UIImage存储CGImage不包含的几个属性,包括alignmentRectInsets

Note that the alignmentRectInsets property exists to influence how auto layout poses a UIImageView containing the image. 请注意,存在alignmentRectInsets属性可影响自动布局如何构成包含图像的UIImageView A UIImageView sets its own alignmentRectInsets from the alignmentRectInsets of its UIImage . UIImageView从其UIImagealignmentRectInsets设置自己的alignmentRectInsets Auto layout then uses the alignment rect when computing the frame of the UIImageView . 然后,自动布局在计算UIImageView的框架时使用对齐矩形。

Auto layout doesn't work with layers, so a CALayer has no need for a alignmentRectInsets property. 自动布局不适用于图层,因此CALayer不需要alignmentRectInsets属性。

In other words, what you're asking for doesn't really make sense. 换句话说,您要的东西没有任何意义。

If you revise your question to explain what visual effect you want, I might be able to offer advice on how to get it. 如果您修改问题以解释所需的视觉效果,我也许可以提供有关如何获得视觉效果的建议。

Try this way : 尝试这种方式:

let resizable = img.resizableImage(withCapInsets: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8), resizingMode: .stretch) 让resizable = img.resizableImage(withCapInsets:UIEdgeInsets(顶部:8,左侧:8,底部:8,右侧:8),resizingMode:.stretch)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM