首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Iphone MultiThreading --我如何从后台线程更新界面(主线程)中的事件?

Iphone MultiThreading --我如何从后台线程更新界面(主线程)中的事件?
EN

Stack Overflow用户
提问于 2010-01-13 06:27:46
回答 2查看 1.2K关注 0票数 1

1.我的mainView是UIScrollView。我必须加载9个图像作为瓷砖到它。

2.我将函数作为一个不同的线程调用,以获取9幅图像并将其加载到接口(主线程)中。通过这样做,接口保持响应性。

3.但是问题是,即使在加载图像时,在整个线程完成之前,它们也不会在UIScrollView中显示。但是,如果我在界面上做了一些操作,我就能够看到每个操作都被加载和显示。

  1. 意味着我必须在每个图像加载到它之后从后台线程更新接口(主线程)。

我该怎么做呢?我给出了用于将图像加载到UIScrollView的代码,我使用不同的线程调用该代码。

代码语言:javascript
运行
复制
- (void)setUpDisplay:(NSArray *)array 
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

myScrollView.userInteractionEnabled = YES;
myScrollView.multipleTouchEnabled = YES;

int x=0,y=0;
CGRect frame;
for (i = 0; i < 3; i++)
{
    if(j == 3)
    {
        x=x+256;
        y = y-768;
    }
    for (j = 0; j < 3; j++) {
        imageView = [[UIImageView alloc] initWithImage:[self getCachedImage:[NSString stringWithFormat:@"url to fetch images"]]];
        frame = [imageView frame];
        frame.origin.x = x;
        frame.origin.y = y;
        [imageView setFrame:frame];
        imageView.userInteractionEnabled = YES;
        imageView.multipleTouchEnabled = YES;
        [myScrollView addSubview:imageView];
        y = y+256;

        [imageView release];
        imageView=nil;
    }
}

[pool release];
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-01-13 07:19:30

您可能需要尝试在每次图像加载完成后在主线程上调用myScrollView setNeedsDisplay。

票数 0
EN

Stack Overflow用户

发布于 2010-01-13 06:31:45

这种NSObject方法通常是有用的:

代码语言:javascript
运行
复制
[someObject performSelectorOnMainThread:@selector(updateUI:) withObject:anotherObject waitUntilDone:YES /* or NO */];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2054868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档