Re: Sending image from separate thread back to AppController
Re: Sending image from separate thread back to AppController
- Subject: Re: Sending image from separate thread back to AppController
- From: Shaun Wexler <email@hidden>
- Date: Wed, 21 Jul 2004 00:04:58 -0700
On Jul 20, 2004, at 1:44 PM, Andrew Duncan wrote:
My AppController creates a new thread (call it Thread2), which runs
and, in its own good time, creates an NSImage. Now I want to tell the
main (UI) thread to display this image. Of course, Thread2 knows
nothing about the outlets to the NSImageView, so it has to ask the
AppController to deal with this.
What's the Best Practice for this one? First impulse is to give the
new thread a pointer back to the AppController. Then Thread2 can call
a method on the AppController, sending the image (pointer) and having
the AppController call setNeedsDisplay on the imageView.
But maybe there's some cool notification scheme or value observer or
something in Panther that I don't know about. I'm sure this is an FAQ
and would appreciate hearing the frequent answer.
extern NSString *myImageIsReadyNotificationName;
...method to be called by 2nd thread, with its completed image:
- (void)notifyWithImage:(NSImage *)image
{
NSNotification *notification = [NSNotification
notificationWithName:myImageIsReadyNotificationName object:image];
[[NSNotificationCenter defaultCenter]
performSelectorOnMainThread:@selector(postNotification:)
withObject:notification waitUntilDone:NO];
}
...somewhere in your AppController setup (-init, thread creation, etc),
register for the notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myImageIsReady:) name:myImageIsReadyNotificationName
object:nil];
.. and finally:
- (void)myImageIsReady:(NSNotification *)notification
{
[myImageView setImage:(NSImage *)[notification object]];
}
Simple? ;)
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.