2010-04-23 102 views
2

我已将视频的视频和缩略图图像存储在“文档”文件夹中。我已经在plist的路上。在plist中,我使用了一个数组,并向数组添加了目录。 而在我的字典存储关键的ImagePath图像路径如何将图像从plist加载到UITableView中?

/Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/9E6E8B22-C946-4442-9209-75BB0E924434/Documents/image1 

视频

/Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/9E6E8B22-C946-4442-9209-75BB0E924434/Documents/video1.mp4 

关键文件路径。

我用下面的代码,但它不工作。我只尝试图像。我需要在每个单元格的表格中加载图像。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

     [cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; 
     UIImageView *image2; 
     image2.frame = CGRectMake(0.0f, 0.0f, 80.0f, 80.0f); 
     image2.backgroundColor = [UIColor clearColor]; 

    } 
    NSDictionary *dictOfplist = [cells objectAtIndex:indexPath.row]; 
    NSString *path = [dictOfplist objectForKey:@"imagePath"]; 
    NSLog("path: %@", path); 
    return cell; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"Library"; 
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStyleBordered target:self action:@selector(close:)]; 

    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"details" ofType:@"plist"]; 
    contentArray = [NSArray arrayWithContentsOfFile:plistPath]; 

    cells = [[NSMutableArray alloc]initWithCapacity:[contentArray count]]; 

    for(dCount = 0; dCount < [contentArray count]; dCount++) 
     [cells addObject:[contentArray objectAtIndex:dCount]]; 
} 

我得到的图像的路径是从plist。
但是,如何从路径中提取图像。
输出OD的NSLog:是

path: /Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/9E6E8B22-C946-4442-9209-75BB0E924434/Documents/snook.jpg 

如何从它和存储获得的图像cell.imageView.image?
我该如何做这项工作。 谢谢。

回答

0

我看到你的代码的一些问题:

  • 当你创建的UITableViewCell实例,您创建一个的UIImageView image2不分配,因此出现内存泄漏。不正常吗?
  • 您不需要通过标记获取对单元格的UIImageView的引用。一个UITableViewCell已经有一个UIImageView,它的图像可以通过cell.imageView.image属性来设置。

你在哪里加载UIImage?您说您正在存储图像路径,但没有用于从此路径加载图像的代码。

+0

我认为在plist中传递字典的键值给出了路径。我的错。我将该路径存储在字典中的键名为imagePath的plist中。我们如何才能从plist到我们的代码?我会检查我的代码并更正它。 谢谢。 – 2010-04-23 08:37:01

+0

我得到了图像的路径。但如何从中获得图像? Th path is path:/ Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/9E6E8B22-C946-4442-9209-75BB0E924434/Documents/snook.jpg 如何显示图像snook.jpg细胞中的路径? – 2010-04-23 09:47:29

+1

使用UIImage的“imageWithContentsOfFile:”方法: UIImage * img = [UIImage imageWithContentsOfFile:path]; – 2010-04-23 11:17:31