繁体   English   中英

通过OperationQueue加载UIImageView的问题

[英]Problem with UIImageView loading via OperationQueue

我使用NSOperationQueue加载UIImageView。

负载从Internet上获取图像,然后将其添加到图像视图。 我的问题是该方法完成了,但是大约需要3秒钟后,图像视图才能实际显示图像或从超级视图中删除图像视图...

- (void)viewDidLoad { NSLog(@"AirportDetailView: viewDidLoad");
    [super viewDidLoad];
    [self.activity startAnimating];
    self.queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImage) object:NULL]; 
    [self.queue addOperation:operation]; 
    [operation release];
}

-(void)loadImage {
        [myAp ImageForAp];

        NSLog(@"ImageFor Ap Ended, %@",myAp.ApDiagram);

        [self.activity stopAnimating];

        if (myAp.ApDiagram==NULL) {
            NSLog(@"Finally Gets Here");
            [self.Diagram removeFromSuperview];
        }
        else {
            NSLog(@"Finally Gets Here with Diag");
            [self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
        }

    }

NSLOG显示前两个日志语句之间的延迟大约3秒,无法理解原因。

更新了我的最新代码……

-(void)loadImage {

    [myAp ImageForAp];

    NSLog(@"ImageFor Ap Ended, %@",myAp.ApDiagram);

    [self performSelectorOnMainThread:@selector(UpdateUI) withObject:nil waitUntilDone:NO];
}

-(void)UpdateUI {

    [self.activity stopAnimating];

    if (myAp.ApDiagram==NULL) {
        NSLog(@"Finally Gets Here");
        [self.Diagram removeFromSuperview];
    }
    else {
        NSLog(@"Finally Gets Here with Diag");
        [self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
    }
}

确保在主线程上运行loadImage方法。 所有UI操作都需要在主线程上进行才能正常工作。 您的代码将需要看起来与此相似。

-(void)loadImage {
    [myAp ImageForAp];
    [self performSelectorOnMainThread:@selector(UpdateUI) withObject:nil waitUntilDone:NO];
}

-(void)UpdateUI {
    [self.activity stopAnimating];

    if (myAp.ApDiagram==NULL) {
        [self.Diagram removeFromSuperview];
    }
    else {
        [self.Diagram setBackgroundImage:myAp.ApDiagram forState:UIControlStateNormal];
    }
}

viewDidLoad内部也可能发生内存泄漏。

self.queue = [NSOperationQueue new]; 应该更改为

self.queue = [[[NSOperationQueue alloc] init] autorelease];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM