herres
herres

Reputation: 53

Update UIImageView from a delegate

I have a Question regarding the UIImageView. I have a ObjectiveC-Class that contains a UIImageView and receive a callback from a delegate. From the delegate I get the UIImage that I want to insert to my UIImageView. But when I set the image it won't be shown.

@interface MyViewController ()
@property(nonatomic,strong) UIImageView* myImageView;
@implementation MyViewController
-(void) viewDidLoad
{
[super viewDidLoad];
self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,400,400)];
[self.view addSubview:self.myImageView]; 

-(void)frameworkService:(FrameworkService *)frameworkService 
callbackfromDelegate:(UIImage*) myImage 
{
self.myImageView.image = myImage; 
}




}

Upvotes: 1

Views: 76

Answers (2)

RobP
RobP

Reputation: 9522

If the callback is not on the main thread, UI won't update. But dispatch_async is unnecessary, just call:

[self.myImageView performSelectorOnMainThread:setImage: withObject:myImage waitUntilDone:NO];

See https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/doc/uid/20000050-CJBEHAEF

Upvotes: 0

rafaperez
rafaperez

Reputation: 1038

You can only update the UI from the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    ... update here your ui ..   
});

Upvotes: 2

Related Questions