从文档目录加载图像

从文档目录加载图像

本文介绍了从文档目录加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法显示我的图像(.png)。

I cant get my Image(.png) to display.

我检查它是否存在于我使用的路径中,它确实存在。但是没有图像出现。

I check that it exists at the path I use, and it does. But no image appears.

以下是我一直在尝试的事情:

Here is what I've been trying:

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir  = [documentPaths objectAtIndex:0];

NSString *outputPath    = [documentsDir stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]];

NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];



NSString *imgPath = [[outputPath stringByReplacingOccurrencesOfString:@"mov" withString:@"png"]retain];


if ([fileManager fileExistsAtPath:imgPath])
{
    NSLog(@"FOUND IMG");
    NSLog(imgPath);
}


NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imgPath]];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 120)];
[imgView setImage:thumbNail];

[cell addSubview:imgView];
[imgView release];

确认文件存在的调试输出

The debug output that confirms the file exists

FOUND IMG
2011-12-26 12:42:23.066 iGeo2[412:707] /var/mobile/Applications/1A2B0D85-CDB1-4E4B-
9509-EF68B1A0E720/Documents/0.009000.png

我有点困惑。有谁看到我犯了错误?

I'm a little baffled. Anybody see where I've made the mistake?

非常感谢,
-Code

Many Thanks,-Code

我已经尝试过其他一些方法,但什么都没有出现。

I've tried a few other methods, but nothing ever appears.

推荐答案

你应该使用 [NSURL fileURLWithPath :imgPath] 而不是 [NSURL URLWithString:imgPath] 。图片路径不是有效网址,需要 file:// 前缀。

You should use [NSURL fileURLWithPath:imgPath] instead of [NSURL URLWithString:imgPath]. Image path is not a valid url, it needs a file:// prefix.

这篇关于从文档目录加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:18