Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-21-2009, 07:00 PM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 125
Default 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.
bhearn is offline   Reply With Quote
Old 03-22-2009, 01:02 AM   #2 (permalink)
ftm
FasterThanMonkeys.com
 
Join Date: Mar 2009
Location: Southern California
Posts: 523
Send a message via AIM to ftm
Default

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.
__________________


Website: http://fasterthanmonkeys.com

iScore Baseball Scorekeeper Top baseball scorekeeping app for iPhone
Bug Squash Reached #1 kids game in 15+ countries including USA, now with OpenFeint
Jam Packed! Fun puzzle game for all ages
Jam Packed Christmas Holiday puzzle game!
iScore Basketball Scorekeeper Best basketball scorekeeping application
ftm is offline   Reply With Quote
Old 03-22-2009, 01:53 AM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 125
Default

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.
bhearn is offline   Reply With Quote
Old 08-08-2009, 08:43 PM   #4 (permalink)
New Member
 
Join Date: Aug 2009
Posts: 1
Default

Hey,

did you ever find a solution to your question? I have exactly the same problem.

All the best
Christoph
cvogelbusch is offline   Reply With Quote
Old 08-11-2009, 06:56 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 50
Default

Quote:
Originally Posted by cvogelbusch View Post
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
andy65 is offline   Reply With Quote
Old 10-26-2009, 10:19 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 3
Default

Quote:
Originally Posted by andy65 View Post
Yes, we also have the same issue here. Please keep up posted.

Andy
iPhone App Icon Design
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.
Toro Liu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,768
Threads: 89,201
Posts: 380,576
Top Poster: BrianSlick (7,129)
Welcome to our newest member, lucileadair
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:04 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0