通过OperationQueue加载UIImageView的问题

发布于 2024-10-31 17:27:44 字数 1600 浏览 3 评论 0原文

我使用 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];
    }
}

I load a UIImageView using an NSOperationQueue.

The load fetches an image from the Internet and then adds it to an image view. The problem I have is that the method finishes but it takes about 3 seconds later for the image view to actually either show the image or remove the image view from the superview...

- (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];
        }

    }

The NSLOG shows a delay between the first two log statements of about 3 seconds can't understand why....

Updated with my Latest Code......

-(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];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2024-11-07 17:27:44

确保 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];

Make sure that the loadImage method is being run on the main thread. All UI operations need to happen on the main thread to work as expected. Your code will need to look similar to this.

-(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];
    }
}

There is also a possible memory leak inside of viewDidLoad.

self.queue = [NSOperationQueue new]; should be changed to

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文