Three20 TTURLRequest 同步缓存(使其异步或线程化)?

发布于 2024-10-29 03:07:52 字数 2202 浏览 2 评论 0原文

我目前正在使用 Three20 库来为 iPhone 项目实现一些缓存。它对加快我的表格视图和页面加载速度有很大帮助,但是我遇到了一个小问题:

我对 TTURLImageResponse 进行了子类化,以在缓存之前调整远程检索的图像的大小/裁剪图像,以便每次我从服务器/缓存取回它们时,我不必调整它们的大小。这工作得很好,图像加载得很快,但是当图像从 Three20 库中的缓存返回时,它们会同步返回。当触发从缓存返回的 10-20 个图像请求时,这会导致明显的延迟。由于它们是同步返回的,因此它们会阻塞我的 UI 线程,并且用户需要等待几秒钟才能看到从缓存返回的图像。

我尝试通过执行以下操作来对操作进行线程化:

- (void) setupImages
{   

    if(imageList != nil &&
       [imageList count] > 0)
    {
        [NSThread detachNewThreadSelector:@selector(fillImages) toTarget:self withObject:nil];
    }
    else
    {
        lblMessage.hidden = NO;
        lblMessage.text = @"There are no images. Try refreshing the current list.";
        lblTitle.text = @"";
        lblAuthor.text = @"";
    }
}

- (void)fillImages
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    int i=0;

    TTURLRequest *request;

    for (MyImage *imageObj in imageList)
    {
        NSString *imageUrl = [[[imageObj thumbnail_lg] image_url] absoluteString];
        if(imageUrl != nil)
        {           
            request = [TTURLRequest requestWithURL:imageUrl delegate:self];
            MyTTURLImageResponse *responseObj = [[[MyTTURLImageResponse alloc] init] autorelease];
            responseObj.index = i;

            request.cachePolicy = TTURLRequestCachePolicyDisk;
            request.response = responseObj;
            request.httpMethod = @"GET";
            [request send];

            i++;
        }
    }

    [pool release];
}

//When the fillImages selector is spawned on its own thread it does not callback to this method, therefore images are never loaded!!
- (void)requestDidFinishLoad:(TTURLRequest*)request 
{
    MyTTURLImageResponse *response = request.response;

    UIImage *loadedImage = response.image;
    int imageIndex = response.index;

    //this now happens as the image is cached :)
    //loadedImage = [loadedImage cropCenterAndScaleImageToSize:CGSizeMake(200, 200)];

    [myImageDisplay setImage:loadedImage forIndex:imageIndex];
}

但是线程似乎不起作用(回调位于主线程上...)。有谁知道一种从 Three20 缓存中异步检索的方法或一种线程化 Three20 TTURLRequest 的方法,以便它至少在后台执行此操作?

I'm currently working with the Three20 library to implement some caching for an iPhone project. It has been a huge help for speeding up my table views and page loads, however I'm running into a small issue:

I have subclassed TTURLImageResponse to resize/crop images that are retrieved remotely before caching them so that I don't have to resize them each time I get them back from the server/cache. This works very well and the images load quite quickly, however when images are returned from the cache in the Three20 library they are returned synchronously. This results in a noticeable delay when firing off 10-20 image requests that come back from the cache. Since they are coming back synchronously, they are blocking my UI thread and the user is left waiting for a couple seconds before they see the images that came back from the cache.

I have attempted to thread the operation by doing the following:

- (void) setupImages
{   

    if(imageList != nil &&
       [imageList count] > 0)
    {
        [NSThread detachNewThreadSelector:@selector(fillImages) toTarget:self withObject:nil];
    }
    else
    {
        lblMessage.hidden = NO;
        lblMessage.text = @"There are no images. Try refreshing the current list.";
        lblTitle.text = @"";
        lblAuthor.text = @"";
    }
}

- (void)fillImages
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    int i=0;

    TTURLRequest *request;

    for (MyImage *imageObj in imageList)
    {
        NSString *imageUrl = [[[imageObj thumbnail_lg] image_url] absoluteString];
        if(imageUrl != nil)
        {           
            request = [TTURLRequest requestWithURL:imageUrl delegate:self];
            MyTTURLImageResponse *responseObj = [[[MyTTURLImageResponse alloc] init] autorelease];
            responseObj.index = i;

            request.cachePolicy = TTURLRequestCachePolicyDisk;
            request.response = responseObj;
            request.httpMethod = @"GET";
            [request send];

            i++;
        }
    }

    [pool release];
}

//When the fillImages selector is spawned on its own thread it does not callback to this method, therefore images are never loaded!!
- (void)requestDidFinishLoad:(TTURLRequest*)request 
{
    MyTTURLImageResponse *response = request.response;

    UIImage *loadedImage = response.image;
    int imageIndex = response.index;

    //this now happens as the image is cached :)
    //loadedImage = [loadedImage cropCenterAndScaleImageToSize:CGSizeMake(200, 200)];

    [myImageDisplay setImage:loadedImage forIndex:imageIndex];
}

However the threading doesn't appear to be working (the callback is on the main thread...). Does anyone know of a way to retrieve from the Three20 Cache either ASYNCHRONOUSLY or a way of threading the Three20 TTURLRequest so that it's at least doing this in the background?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文