如何使UI响应所有的时间和做后台更新? [英] How to make ui responsive all the time and do background updating?

查看:97
本文介绍了如何使UI响应所有的时间和做后台更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建它显示每页8缩略图应用程序,它可以有n页。每个缩略图是UIViews并添加到UIScrollView的。但是我一直在使用苹果的样品code语言实现的寻呼。

I am creating a application which displays 8 thumbnails per page and it can have n pages. Each of these thumbnails are UIViews and are added to UIScrollView. However i have implemented Paging using the Apple sample code.

的概率:


  1. 每个缩略图(UIView的)需要150
    被创建并添加到的毫秒
    滚动视图

  2. 因此,对于3页需要可怕
    创建和加入大量的时间
    在UI滚动视图。

  3. 在这一点上,滚动视图不是很respsonsive,这是非常生涩,并给出了不好的用户体验

  4. 如何创建缩略图并将其添加到的UIScrollView不影响触摸反应?我希望他们能够独立于主线程是resposible处理触摸事件(我想)的运行。

此外,我想提一提,创建缩略图,当我触发图像和委托方法的异步下载时被调用下载完成。

Also i would like to mention that when a thumbnail is created i trigger a Async download of the image and the delegate method is called when download is complete.

让我知道我不得不作出这样的响应和更新UI,而不会影响触摸操作的选项。页面控制工作正常缩略图方格的延迟加载。

Let me know the the options i have to make this more responsive and update UI without affecting the touch operations. The page control works fine with lazy loading of the grid of thumbnails.

TIA,

普利文小号

推荐答案

大中央调度是易于使用的后台加载。但是GCD是只为iOS4的了。如果你有支持IOS3,performSelectorInBackground / performSelectorOnMainThread或NSOperationQueue是有益的。

Grand Central Dispatch is easy to use for background loading. But GCD is only for after iOS4. If you have to support iOS3, performSelectorInBackground/performSelectorOnMainThread or NSOperationQueue are helpful.

和,小心几乎UIKit类不是线程安全的,除了绘制到图形上下文。例如,UIScrollView的是不是线程安全的,UIImage的imageNamed:是不是线程安全的,但UIImage的imageWithContentsOfFile:是线程安全的。

And, be careful almost UIKit classes are not thread-safe except drawing to a graphics context. For example, UIScrollView is not thread-safe, UIImage imageNamed: is not thread-safe, but UIImage imageWithContentsOfFile: is thread-safe.

dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_queue_t concurrentQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_async(concurrentQueue, ^{

    dispatch_apply([thumbnails count], concurrentQueue, ^(size_t index) {

        Thumbnail *thumbnail = [thumbnails objectAtIndex:index];
        thumbnail.image = [UIImage imageWithContentsOfFile:thumbnail.url];

        dispatch_sync(mainQueue, ^{

            /* update UIScrollView using thumbnail. It is safe because this block is on main thread. */

        });
    }

    /* dispatch_apply waits until all blocks are done */

    dispatch_async(mainQueue, ^{
        /* do for all done. */
    });
}

这篇关于如何使UI响应所有的时间和做后台更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆