 |
 |
|
 |
03-21-2009, 06:00 PM
|
#1 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 125
|
background threads and graphics
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.
|
|
|
03-22-2009, 12:02 AM
|
#2 (permalink)
|
|
FasterThanMonkeys.com
iPhone Dev SDK Supporter
Join Date: Mar 2009
Location: Southern California
Posts: 521
|
Updates to the UI should be done in the main thread. Have you tried using a call like the following from your background thread:
Code:
[self performSelectorOnMainThread:@selector(doMyResize:) withObject:nil waitUntilDone:false];
That should call the doMyResize method using the main thread... just put your UI updating code in that method and it should work without crashing.
|
|
|
03-22-2009, 12:53 AM
|
#3 (permalink)
|
|
New Member
Join Date: Aug 2008
Posts: 125
|
Thanks, but I'm not updating the UI; I'm resizing an image into an offscreen bitmap. Also, it takes too long to do the resize (~2 seconds) to run on the main thread; it's unacceptable to shut the interface down that long.
However, I think I have mostly solved the problem. It looks to me like you can do this safely in a background thread as long as you use only Core Graphics calls, and not the UI calls. UIGraphicsBeginImageContext() does indeed push a new CG context onto a stack, which is a problem.
But by explicitly calling CGBitmapContextCreate(), CGContextDrawImage(), and CGBitmapContextCreateImage(), you can resize the image without messing with the UIKit's graphics context stack.
The problem now is dealing with the damn imageOrientation field of the image, which seems to be handled automatically when you use the UIKit.
Also it took me a while to realize that if I wanted to get meaningful values in this field for images taken from the camera in-app, I had to first enable device orientation notifications.
|
|
|
08-08-2009, 07:43 PM
|
#4 (permalink)
|
|
New Member
Join Date: Aug 2009
Posts: 1
|
Hey,
did you ever find a solution to your question? I have exactly the same problem.
All the best
Christoph
|
|
|
08-11-2009, 05:56 AM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jul 2009
Posts: 49
|
Quote:
Originally Posted by cvogelbusch
Hey,
did you ever find a solution to your question? I have exactly the same problem.
All the best
Christoph
|
Yes, we also have the same issue here. Please keep up posted.
Andy
iPhone App Icon Design
|
|
|
10-26-2009, 09:19 PM
|
#6 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 3
|
Quote:
Originally Posted by andy65
|
I encounter the same problem, is there any solution ?
I found something in Apple's official documents, but it is used for NSView class.
I cannot find the method named lockFocusIfCanDraw and unLockFocus in UIView or other place.
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 429 |
| 41 members and 388 guests |
| acerola, aoredsson, aryaxt, beginer2007, benoitr007, BolderImage, Chilibird, dcinqc, DenVog, develsolutions, Dorald, dre, Exaviorn, fordprefect77, georgeburns, Hanks2000, harrytheshark, HemiMG, iTrackiGiveaway, jbro, LemonMeringue, MartinIngvar, meosoft, Meoz, mikeyb, MiniRobinho, nibby, noobAppDeveloper, oyvindj, Pwsoccer, rocketman240, scotopia, Shlice Ideas, snowboarderz69, spikeyfish2, stack_trace, Tambourin, thegreyit, upperhouse, walkman2001, warcrow |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,300
Threads: 39,100
Posts: 171,423
Top Poster: smasher (2,575)
|
| Welcome to our newest member, lending |
|