簡體   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