将图像异步加载到tableview单元格中

时间:2012-02-27 17:11:34

标签: image asynchronous uitableview nsinvocation

在互联网上搜索了几周之后这里的很多帖子也在堆栈中,我似乎无法找到在我的项目中实现这一点的方法 我的Dropbox帐户中有一个plist。其中一个词典中名为“cellPic”的词典中的一个字符串 我想在表格单元格中显示我在Dropbox中存储的图片 plist被下载到我的docs路径:

-(void)readPlistFromServerAndSaveToDocs
    {
    //save plist from server to docs:
    NSArray *tempArr1 = [[NSArray alloc]initWithContentsOfURL:
                         [NSURL URLWithString:@"http://dl.dropbox.com/u/4082823/AppsFiles/Black/stores.plist"]];
    NSString *error;
    NSString *rootPath =
    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"stores.plist"];
    NSArray *tmpArr = [NSArray arrayWithArray:tempArr1];
    NSData *tmpData = [NSPropertyListSerialization dataFromPropertyList:tmpArr
                                                             format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    NSLog(@"plistPath=%@",plistPath);

    if(tmpData)
    {
        [tmpData writeToFile:plistPath atomically:YES];
    }

    else 
    {
        NSLog(@"%@",error);
    }

    //Announcement if there is no server connection: 

    if(tempArr1.count == 0)
    {
        [self plistMessages];
    }
}

到目前为止一切都还好......

在我的TableViewController下,我希望为单元格实现异步图像加载。

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

我的目标是异步读取/下载图像,但我不知道它是怎么回事 - 我需要在我的文档路径中读取我的plist中的字符串,其中包含pic的完整url地址或者我需要从服务器下载图片到我的文档&然后把它读到单元格?
在我的情况下 - 细胞的位置正在改变,它们按用户位置排序(最近的用户存储在桌面上),这就是为什么我认为我需要从我的plist中读取它。
这是我的照片网址之一:http://dl.dropbox.com/u/4082823/AppsFiles/Black/ramat-hasharon.png
什么是最好的方法来做到这一点&如何一次下载所有png图像(如果需要)? 我已经尝试用这个教程来完成它,但它根本没有 http://www.switchonthecode.com/tutorials/loading-images-asynchronously-on-iphone-using-nsinvocationoperation
帮助将受到赞赏!!
谢谢!

1 个答案:

答案 0 :(得分:2)

查看Apple的示例代码LazyTableImages