John Qualis
John Qualis

Reputation: 1733

updating uiimages dynamically

Any examples available that update UIImageView images dynamically at a high rate? These images are received on a IP connection in a NSThread. But they don't update when I do imageView.image = newImageRecd;

EDIT: All data is recd on main thread

Appreciate any help/pointers.

Upvotes: 0

Views: 802

Answers (2)

ill_always_be_a_warriors
ill_always_be_a_warriors

Reputation: 1566

For iOS 4 and later, you can use Grand Central Dispatch (GCD) to load images on a background thread. Here's a link that explains this process fully:

http://blog.slaunchaman.com/2011/02/28/cocoa-touch-tutorial-using-grand-central-dispatch-for-asynchronous-table-view-cells/

The tutorial above provides this code:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

dispatch_async(queue, ^{
    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

    dispatch_sync(dispatch_get_main_queue(), ^{
        [[cell imageView] setImage:image];
        [cell setNeedsLayout];
    });
});

I've been using this myself to load images from a remote server, and it's working great.

Upvotes: 1

Evan Mulawski
Evan Mulawski

Reputation: 55334

UI updates need to be performed on the main thread. If you are running the connection on a different thread, use:

[self performSelectorOnMainThread:@selector(updateImageView:) withObject:newImageRecd waitUntilDone:FALSE];

where updateImageView: is the method used to set the image view's image property:

-(void)updateImageView:(UIImage *)image
{
    [self.imageView setImage:image];
}

Upvotes: 0

Related Questions