1minus1
1minus1

Reputation: 25

UIImageView fails to update repeatedly

I have a view controller, which calls the following in viewDidAppear:

[self updateCameraImage];
[cameraImageView setImage:lastCameraImage];

That works great. In the background, the updateCameraImage method sends a message to the server and retrieves image data, then converts that data to a UIImage. It then sets self.lastCameraImage to this UIImage. The second line set this image to cameraImageView.image and the new image shows on screen. This UIImageView was added in a storyboard and added to the header as a property.

@property (nonatomic, strong) UIImage *lastCameraImage;
@property (weak, nonatomic) IBOutlet UIImageView *cameraImageView;

I would like this to continually update. The problem is that if I do something like the following, it doesn't actually show a new image in the view until the last time setImage is called. I know the image is being retrieved, so I can't tell why it's not working.

[self updateCameraImage];
[cameraImageView setImage:lastCameraImage];
[self updateCameraImage];
[cameraImageView setImage:lastCameraImage];
[self updateCameraImage];
[cameraImageView setImage:lastCameraImage];

Any help would be great! Thanks.

Upvotes: 0

Views: 60

Answers (1)

mspensieri
mspensieri

Reputation: 3521

If you're running this code on the main thread, and your updateCameraImage function is synchronous, then you'll be blocking the UI until all code completes. Try performing the fetch in the background, then dispatch back to the main thread to update the UI.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self updateCameraImage];

    dispatch_async(dispatch_get_main_queue(), ^{
        [cameraImageView setImage:lastCameraImage];
    });
});

Upvotes: 1

Related Questions