在UITableView iPhone SDK中加载图片 [英] lazy loading images in UITableView iPhone SDK

查看:110
本文介绍了在UITableView iPhone SDK中加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在tableview中为图像实现延迟加载概念,以便最初为用户提供文本数据,然后提供图像。

I need to implement the lazy loading concept for the images in my tableview, so that the user will be provided with the textual data initially and later the images.

如何在我的应用程序中实现此功能..需要帮助..请

How can i implement this in to my app.. help needed.. please

提前致谢

Shibin

推荐答案

我为矿山项目创作的内容如下;
按UITableViewCell + Async.h中的类别扩展UITableViewCell类
(如果您不确定Obj C中的类别是什么,请参阅一些示例)

What i have created for a project of mine works as follows; Extend the UITableViewCell class by a category in "UITableViewCell+Async.h" (See some examples if you are not sure what the category thing in Obj C is)

@interface UITableViewCell (Async)

-(void)loadAsyncImage:(NSString*)url withIndex:(NSInteger)index inWidth:(NSInteger)width inHeight:(NSInteger)height;
-(void)loadAsyncBackground:(NSMutableArray*)parameters;

@end

然后在实现文件UITableViewCell + Async中。 m

And then in the implementation file "UITableViewCell+Async.m"

#import "UITableViewCell+Async.h"

@implementation UITableViewCell (Async)

-(void)loadAsyncImage:(NSString*)url 
            withIndex:(NSInteger)index 
              inWidth:(NSInteger)width 
             inHeight:(NSInteger)height {

    NSMutableArray* parameters = [NSMutableArray arrayWithCapacity:2];
    [parameters addObject:url];
    [parameters addObject:[NSNumber numberWithInteger:index]];
    [parameters addObject:[NSNumber numberWithInteger:width]];
    [parameters addObject:[NSNumber numberWithInteger:height]];

    self.imageView.tag = index;
    [self performSelectorInBackground:@selector(loadAsyncBackground:) withObject:parameters];

}

-(void)loadAsyncBackground:(NSMutableArray*)parameters {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString* url = [parameters objectAtIndex:0];
    NSInteger index = [[parameters objectAtIndex:1] integerValue];
    NSInteger width = [[parameters objectAtIndex:2] integerValue];
    NSInteger height = [[parameters objectAtIndex:3] integerValue];

    UIImage* image = [Utils getImageResized:url inSize:CGSizeMake(width, height)];

    if (self.tag==index) {
        self.imageView.image = image;
        [self setNeedsLayout];
    }

    [pool release];
}

@end

这基本上增加了一项功能UITableViewCell用于在新的后台线程中加载图像,调整图像大小并将其设置为imageview。添加检查标记以查看单元格是否仍在等待图像,因为它可以重复使用,而图像的另一个线程可能正在为该重用单元格下载另一个图像...

This basically adds a functionality to the UITableViewCell to load an image in a new background thread, resize the image and set it to the imageview. Checking the tag is added to see if the cell is still waiting for the image, since it can be re-used and another thread for the image could be downloading another image for that re-used cell...

以上代码中的函数,签名为;

The function in the above code with the signature of;

+(UIImage*)getImageResized:(NSString*)url inSize:(CGSize)size;

检查图像的本地缓存,如果不在缓存中则从Web下载图像,保存到本地缓存,调整给定大小的图像大小并返回图像,所有这些都是在同步(阻塞)方法调用中完成的。由于这已经是后台线程,因此对此操作没有任何伤害。当方法返回图像时,如果它仍然具有相同的标记(不再用于其他行),则将其设置为单元格的imageview。

checks a local cache of images, downloads the image from the web if not in the cache, saves it to local cache, resizes the image in the given size and returns the image, all done in a sync(blocking) method call. Since this is already the background thread, no harm blocking it for this operation. When the method returns the image, it is set to the cell's imageview if it still has the same tag(not re-used for some other row)

在cellForRowAtIndexPath方法中你可以添加新的类别,你应该完成;

In the cellForRowAtIndexPath method you can add the new category and you should be done;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"......."];
    if (cell == nil) {
................
    }
................
    [cell loadAsyncImage:deal.logo withIndex:indexPath.row inWidth:40 inHeight:40];
................

    return cell;
}

这篇关于在UITableView iPhone SDK中加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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