【问题标题】:how can load images from plist in to UITableView?如何将图像从 plist 加载到 UITableView?
【发布时间】:2010-04-23 06:46:49
【问题描述】:

我已将视频和视频的缩略图存储在 Documents 文件夹中。我已经在 plist 中给出了路径。在 plist 中,我获取了一个数组,并将目录添加到数组中。 在字典中我存储了图像路径

/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 中?
我怎样才能使这项工作。 谢谢。

【问题讨论】:

    标签: cocoa-touch iphone-sdk-3.0 uitableview uiimageview


    【解决方案1】:

    我发现您的代码中存在一些问题:

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

    你在哪里加载 UIImage ?你说你存储的是图片路径,但是没有从这个路径加载图片的代码。

    【讨论】:

    • 我认为在 plist 中传递字典的键值给出了路径。我的错。我将路径存储在 plist 中的字典中,用于键值 imagePath。我们如何获得从 plist 到我们的代码的路径?我会检查我的代码并更正它。谢谢。
    • 我得到了图片的路径。但是如何从中获取图像?路径是路径:/Users/srikanth/Library/Application Support/iPhone Simulator/User/Applications/9E6E8B22-C946-4442-9209-75BB0E924434/Documents/snook.jpg 如何从单元格中的路径显示图像 snook.jpg ?
    • 使用UIImage的“imageWithContentsOfFile:”方法: UIImage *img = [UIImage imageWithContentsOfFile:path];
    • 我做到了,但它不起作用。它没有在单元格中显示图像。当我调试时,img 显示为 0*0。请问有什么建议吗?谢谢。
    • 非常感谢 Laurent 先生。我终于得到了正确的解决方案。而且我一直搞砸了,没有清楚地知道我需要什么以及我应该做什么。现在,我将图像保存在 Documents 文件夹中,并将文件名保存在 plist 中。我用这个文件名访问了它。我使用了你建议我的 UIImage 属性。现在我在表格单元格中获得了图像。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多