How does one apply UIEdgeInsets (or some variation thereof) to a 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.
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).
let image = someUIImage.cgImage!
image. // can't apply any inset here
layer.contents = image
Workaround?
You cannot “apply
UIEdgeInsets” to aCGImagebecause aCGImagedoesn't have analignmentRectInsetsproperty. AUIImagestores several properties that aCGImagedoes not, includingalignmentRectInsets.Note that the
alignmentRectInsetsproperty exists to influence how auto layout poses aUIImageViewcontaining the image. AUIImageViewsets its ownalignmentRectInsetsfrom thealignmentRectInsetsof itsUIImage. Auto layout then uses the alignment rect when computing the frame of theUIImageView.Auto layout doesn't work with layers, so a
CALayerhas no need for aalignmentRectInsetsproperty.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.