iOS:如何阻止后台线程更新主线程中的UI?

iOS:如何阻止后台线程更新主线程中的UI?,第1张

概述我实现了一个UITmageView UITableView单元,每个单元通过NSTimer每隔5秒定期刷新一次.每个图像都是从后台线程中的服务器加载的,从后台线程我也通过调用performSelectorOnMainThread更新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) {    }];}
@R_403_6120@ 好…
另一种方法是将“图像请求代码”放入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?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/999411.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存