从委托更新UIImageView

时间:2014-04-09 14:39:08

标签: objective-c uiimageview

我对UIImageView有疑问。 我有一个ObjectiveC-Class,它包含一个UIImageView并从委托接收回调。 从委托中我得到了我想要插入UIImageView的UIImage。但是当我设置图像时,它将不会显示。

@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; 
}




}

2 个答案:

答案 0 :(得分:1)

您只能从主线程更新UI:

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

答案 1 :(得分:0)

如果回调不在主线程上,则UI不会更新。但是dispatch_async是不必要的,只需调用:

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

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