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 aCGImage
because aCGImage
doesn't have analignmentRectInsets
property. AUIImage
stores several properties that aCGImage
does not, includingalignmentRectInsets
.Note that the
alignmentRectInsets
property exists to influence how auto layout poses aUIImageView
containing the image. AUIImageView
sets its ownalignmentRectInsets
from thealignmentRectInsets
of itsUIImage
. Auto layout then uses the alignment rect when computing the frame of theUIImageView
.Auto layout doesn't work with layers, so a
CALayer
has no need for aalignmentRectInsets
property.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.