如何从一些默认图像加载表,直到从互联网iPhone获取原始图像?

时间:2011-07-14 07:57:35

标签: iphone uitableview uiimageview default-value

我有一个uitableview,它将一个图像存储在一个单元格中。它的图像从互联网上加载。问题是应用程序变慢,直到其图像从Internet加载。所以我想放置一个首先加载默认图像的机制,当它获得原始图像时,默认图像将被原始图像替换。

给我任何教程或任何示例代码。

提前致谢。

3 个答案:

答案 0 :(得分:1)

通过其他线程获取您的图像....并使用通知或委托来跟踪图像下载中的任何更改...假设您正在使用通知....然后在您设置为观察者的类中通知重新加载tableView的数据....所以你的表图像将更新到有默认图像的地方....更多细节让我们举一个例子.....制作一个操作类来下载我们称之为的图像拇指....在这个例子中我制作了2个班级 1. PhotoGalleryVC显示拇指列表和每个拇指的一些细节 2. LoadGalleryThumbOp [操作=操作]下载拇指并在完成后发布通知

@protocol LoadGalleryThumbDelegate;
@interface LoadGalleryThumbOp : NSObject{

    NSIndexPath* indexPathInTableView;
    id <LoadGalleryThumbDelegate> delegate;
    NSMutableData *activeDownload;
        NSURLConnection *imageConnection;
    NSString * documentPath;
    BOOL imageDownload;
}
@property (nonatomic, assign) NSIndexPath* indexPathInTableView;
@property (nonatomic, retain) NSMutableData *activeDownload;
@property (nonatomic, retain) NSURLConnection *imageConnection;
@property (nonatomic, assign) id <LoadGalleryThumbDelegate> delegate;
@property (nonatomic, retain) NSString * documentPath;
@property (nonatomic) BOOL imageDownload;
- (void)startDownload;
- (void)cancelDownload;
- (void) persistData:(NSData*) data;
@end

@protocol LoadGalleryThumbDelegate 

- (void)appImageDidLoad:(NSIndexPath *)indexPath;

@end
在LoadGalleryThumbOp.m中

将其作为

@implementation LoadGalleryThumbOp

@synthesize year;
@synthesize indexPathInTableView;
@synthesize delegate;
@synthesize activeDownload;
@synthesize imageConnection,documentPath,imageDownload;

#pragma mark

- (void)startDownload
{
    self.imageDownload = YES;
    self.activeDownload = [NSMutableData data];
    NSFileManager* fm = [NSFileManager defaultManager];
    NSString* galleryDocumentPath = [self.documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"images/thumb.jpg"]];
    if ([fm fileExistsAtPath:galleryDocumentPath]) 
    {
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:galleryDocumentPath ];
         self.gallery.thumpImage = image;
        self.activeDownload = nil;
        [image release];
        self.imageConnection = nil;
         [delegate appImageDidLoad:self.indexPathInTableView];
    }
    else {
        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
                                 [NSURLRequest requestWithURL:
                              [NSURL URLWithString:#error yourImageUrl]] delegate:self];
        self.imageConnection = conn;
        [conn release];
    }
}

- (void)cancelDownload
{
    [self.imageConnection cancel];
    self.imageConnection = nil;
    self.activeDownload = nil;
}


#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.activeDownload appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    self.activeDownload = nil;
    self.imageConnection = nil;
    self.imageDownload = NO;
     [NSString stringWithFormat:@"images/thumb.jpg"]];
    [delegate appImageDidLoad:self.indexPathInTableView];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self persistData:self.activeDownload];    
    self.activeDownload = nil;
    self.imageConnection = nil;
    [delegate appImageDidLoad:self.indexPathInTableView];
}
- (void) persistData:(NSData*) data
{
    NSFileManager* fm = [NSFileManager defaultManager];
    NSString* galleryDocumentPath = [self.documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"images/thumb.jpg"]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:galleryDocumentPath]) 
    {
        NSError* err = nil;
        [fm removeItemAtPath:galleryDocumentPath error:&err];
        if (err)
            NSLog(@"%s:%@",__FUNCTION__,err);
    }
    [fm createFileAtPath:galleryDocumentPath contents:data attributes:nil];
}

此课程将下载您想要的图像,并在下载图像时调用其委托。 现在它在PhotoGalleryVC中的使用部分就像这样使用

@interface PhotoGalleryVC : UIViewController <LoadGalleryThumbDelegate>{

    IBOutlet UITableView* albumListTableView;
    NSMutableDictionary *imageDownloadsInProgress;
    NSArray* allThumbs;
}
@property (nonatomic, retain) NSMutableDictionary *imageDownloadsInProgress;

- (void)appImageDidLoad:(NSIndexPath *)indexPath;
@end

在.m部分

- (void)viewDidLoad {
     self.imageDownloadsInProgress = [NSMutableDictionary dictionary];
     .....

}

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

NSFileManager* fm = [NSFileManager defaultManager];
    NSString* galleryDocumentPath =  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"Gallery/%@/images/thumb.jpg",[someObj.title stringByReplacingOccurrencesOfString:@" " withString:@"_" ]]];
//this above line is just make a seperate folder for each object and store thumbs of that object in that folder ... so make it unique some how
    if (![fm fileExistsAtPath:galleryDocumentPath]) 
    {
        LoadGalleryThumbOp *galleryThumbDownloader = [imageDownloadsInProgress objectForKey:indexPath];
        if (galleryThumbDownloader != nil && galleryThumbDownloader.imageDownload == NO)
        {
            [cell.activityIndicator stopAnimating];
            cell.albumCoverImageView.image = [UIImage imageNamed:@"no_thumb.png"];
        }
        else {
            [cell.activityIndicator startAnimating];                
        }
        [self startIconDownload:temp forIndexPath:indexPath andYear:[NSString stringWithFormat:@"%d",selectedYear]];
    }
    else
    {
        [cell.activityIndicator stopAnimating];
        cell.albumCoverImageView.image = [UIImage imageWithContentsOfFile:galleryDocumentPath];
    }
    return cell ;
}

//The following method see if there is already so downloader that is downloading same image then it simply do nothing else it create a downloader and start it     
- (void)startIconDownload:(Gallery *)gallery forIndexPath:(NSIndexPath *)indexPath  andYear:(NSString*)yr
{
    LoadGalleryThumbOp *galleryThumbDownloader = [imageDownloadsInProgress objectForKey:indexPath];
    if (galleryThumbDownloader == nil) 
    {
        NSString* documentsPath =    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString* nameWithoutSpace = [gallery.title stringByReplacingOccurrencesOfString:@" " withString:@"_" ];
        NSString* galleryDocumentPath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Gallery//%@",nameWithoutSpace]];

        galleryThumbDownloader = [[LoadGalleryThumbOp alloc] init];
        galleryThumbDownloader.documentPath = galleryDocumentPath;
        galleryThumbDownloader.indexPathInTableView = indexPath;
        galleryThumbDownloader.delegate = self;
        [imageDownloadsInProgress setObject:galleryThumbDownloader forKey:indexPath];
        [galleryThumbDownloader startDownload];
        [galleryThumbDownloader release];   
    }
    else if(galleryThumbDownloader.imageDownload == NO)
    {
        if (albumListTableView.dragging || albumListTableView.decelerating) {
            [galleryThumbDownloader startDownload];
        }
    }
}

最后下载特定图像时调用的方法

- (void)appImageDidLoad:(NSIndexPath *)indexPath
{
    CustomCellPhotoGalary* cell = (CustomCellPhotoGalary*)[albumListTableView cellForRowAtIndexPath:indexPath];
    LoadGalleryThumbOp *galleryThumbDownloader = [imageDownloadsInProgress objectForKey:indexPath];

    if (galleryThumbDownloader != nil)
    {
        if (galleryThumbDownloader.imageDownload) 
        {
            [cell.activityIndicator stopAnimating];
            NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];                                
                    NSString* nameWithoutSpace = [temp.title stringByReplacingOccurrencesOfString:@" " withString:@"_" ];
            NSString* galleryDocumentPath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Gallery/%@",nameWithoutSpace]];
            cell.albumCoverImageView.image = [UIImage imageWithContentsOfFile:galleryDocumentPath];
            //galleryThumbDownloader = nil;
        }
        else {
            [cell.activityIndicator stopAnimating];
            cell.albumCoverImageView.image = [UIImage imageNamed:@"no_thumb.png"];
            [self startIconDownload:temp forIndexPath:indexPath andYear:[NSString stringWithFormat:@"%d",selectedYear]];
    }
        [albumListTableView reloadData];
    }
}

woooh .....!这就是一个例子的很多代码 注意:我删除了这么多行,所以这段代码可能不适用于你直接{可能有很多错误..},但我希望你得到幕后的主要想法...... :)

答案 1 :(得分:0)

This示例代码很棒,它教会了我很多。 这使用ASIHttpRequest框架,如果您打算使用这种技术,我建议您使用here中的最新版本,因为示例代码中的版本现在已经很老了。

此示例代码可以帮助您自定义单元格委派等。

答案 2 :(得分:0)

根据上述情况,使用LazyTableImages总是更好。

给出here

的最佳示例