HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS:如何阻止后台线程更新主线程中的UI?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我实现了一个UITmageView UITableView单元,每个单元通过NSTimer每隔5秒定期刷新一次.每个图像都是从后台线程中的服务器加载的,从后台线程我也通过调用performSELEctorOnMainThread更新UI,显示新图像.到现在为止还挺好.

目前,当我不等到当前单元格中加载图像并快速向下滚动到新单元格时,我遇到了问题.然而,该新单元显示先前单元的图像而不是新单元的图像.然下一轮NSTimer会正确显示图像,但这可能会让用户感到困惑.

如果我不重用UITableView的单元格,问题就会消失,但虑到我的应用程序中显示的单元格数量,这不是一个选项.

因此,我能想到的唯一解决方案是,如果我知道用户执行滚动操作,则取消(或终止)将显示旧图像的后台线程.

我想知道这可能不是最好的做法,因此,寻求你的意见.

(我也不能使用SDWebImage,因为我的要求是在从服务器加载的循环中显示一组图像)

// In MyViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath
{
    ...
    NSTimer* timer=[NSTimer scheduledTimerWithTimeInterval:ANIMATION_scheDULED_AT_TIME_INTERVAL 
                                                target:self 
                                              SELEctor:@SELEctor(updateImageInBACkground:) 
                                              userInfo:cell.imageView 
                                               repeats:YES];
    ...
}

- (void) updateImageInBACkground:(NSTimer*)aTimer
{  
    [self performSELEctorInBACkground:@SELEctor(updateImage:)
                       withObject:[aTimer userInfo]];
}  

- (void) updateImage:(AnimatedImageView*)animatedImageView 
{      
    @autoreleasepool { 
        [animatedImageView refresh];
    }
}  

// In AnimatedImageView.m
-(void)refresh
{
    if(self.currenTindex>=self.urls.count)
        self.currenTindex=0;

    ASIhttprequest *request=[[ASIhttprequest alloc] initWithURL:[self.urls objectATindex:self.currenTindex]];
    [request startSynchronous];

    UIImage *image = [UIImage imageWithData:[request responseData]];

    // How do I cancel this operation if I kNow that a user performs a scrolling action,therefore deparTing from this cell.
    [self performSELEctorOnMainThread:@SELEctor(performTransition:)
                       withObject:image
                    waitUntilDone:YES];
}

-(void)performTransition:(UIImage*)anImage
{
    [UIView transitionWithView:self duration:1.0 options:(UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction) animations:^{ 
        self.image=anImage;
        currenTindex++;
    } completion:^(BOOL finished) {
    }];
}

解决方法

好…
另一种方法是将“图像请求代码”放入AnimatedImageView中,并在每次设置新图像URL时使待处理请求无效

// your controller
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath
    {
    ...
    [cell.imageView setDistantImage:imageURL];
    ...    
    }

然后,在你的Cell类中,’setDistantImage’创建一个ASIhttprequest,并使前一个无效

//your ImageView class

@property (...) ASIhttprequest *distantImagerequest;

- (void) setDistantImageUrl:(NSUrl *)imageUrl
{
    //clear prevIoUs request
    [distantImagerequest clearDelegatesAndCancel];
    //set a new request,with the callBACk embedded directly in this ImageView class
    ... 
}

//animation and callBACks methods in this ImageView class too...

大佬总结

以上是大佬教程为你收集整理的iOS:如何阻止后台线程更新主线程中的UI?全部内容,希望文章能够帮你解决iOS:如何阻止后台线程更新主线程中的UI?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。