I need to resize an image smaller in a background thread.
This trick:
Code:
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
often crashes, I think because the change to the graphics context stack can interfere with any drawing on the main thread.
There's also the undocumented API _imageScaledToSize, for which you don't need to (explicitly, anyway) change the graphics context, but this seems to crash occasionally as well.
Any other ways anyone can think of? Unfortunately doing it in the main thread is really not an option. When we get a photo from the photo picker, and it was taken with the camera, it takes a couple of seconds to resize, and we can't freeze the interface for that long.
The docs are not very clear on what kind of drawing is allowable in background threads. There is this:
Quote:
|
The Application Kit is generally thread-safe when drawing with its graphics functions and classes, including the NSBezierPath and NSString classes. Details for using particular classes are described in the following sections. Additional information about drawing and threads is available in Cocoa Drawing Guide.
|
But though this is in the iPhone docs, it seems to apply only to Mac OS X.