I want to stretch an image using "9-slice" scaling, where only the centre of the image is scaled. Exactly like UIImage'sstretchableImageWithLeftCapWidth
method, however I want to do this in a background thread. I understand that this method can only be used in the gui thread.
I'm developing on iOS / iPad / iPhone.
Does anyone have a code snippet, or know of a library that can do this? I'm trying to not re-invent the wheel!
CGContext docs:
Nice blog entry on drawing in the background:
Rotating an image using CGContext:
Since iOS 4, some parts of UIKit are thread-safe; I believe UIGraphics functions and UIImage drawing should work.
If you need to support iOS 3, then it's a bit more difficult. If you want to avoid UIKit entirely (possibly wise), then you'll need to pass the background thread a CGImageRef. There are additional things you might need to worry about; I'm assuming scale
is 1 and imageOrientation
is portrait.
CGBitmapContextCreate()
CGImageCreateWithImageInRect()
.CGContextDrawImage()
CGBitmapContextCreateImage()
+[UIImage imageWithCGImage:]
or +imageWithCGImage:scale:orientation:
(note that in the latter case, images with scale 2 only seem to support portrait orientations; I'm not sure why).Also note that it's significantly better to stick the image in a UIImageView and set contentStretch appropriately, since then the scaling is done on the GPU.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With