Reputation: 2974
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?
Upvotes: 1
Views: 978
Reputation: 385860
You cannot “apply UIEdgeInsets
” to a CGImage
because a CGImage
doesn't have an alignmentRectInsets
property. A UIImage
stores several properties that a CGImage
does not, including alignmentRectInsets
.
Note that the alignmentRectInsets
property exists to influence how auto layout poses a UIImageView
containing the image. A UIImageView
sets its own alignmentRectInsets
from the alignmentRectInsets
of its UIImage
. Auto layout then uses the alignment rect when computing the frame of the UIImageView
.
Auto layout doesn't work with layers, so a CALayer
has no need for a alignmentRectInsets
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.
Upvotes: 2
Reputation: 7892
Try this way :
let resizable = img.resizableImage(withCapInsets: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8), resizingMode: .stretch)
Upvotes: 0