如何显示图像从文档目录到图像视图?

问题描述:

我想从文档目录显示我的图像...我得到了目录的内容。 但是当我想显示图像,它什么也没有显示...我检查了文档目录.. 有图像..但不知道它为什么不显示在UIimage视图?如何显示图像从文档目录到图像视图?

任何人都可以帮我吗?哪里有问题???

我的代码

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil]; 
setImage.image = [UIImage imageWithContentsOfFile:[filePathsArray objectAtIndex:0]]; 

嘿,我得到的答案.....请点击此链接

here

感谢名单大家的宝贵帮助.......

+0

所有在NSDocumentDirectory的形象呢? – Bazinga

+0

你在newDir中传递了什么? –

+0

@ Kobe.o4是的所有图像都在文档目录中...... – Rox

我觉得你很困惑UIImageUIImageView。创建UIImage setImage后,需要将其设置为一个UIImageView:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:setImage]; 

,那么你需要的myImageView添加到您的视图:

[self.view addSubview:myImageView]; 

编辑

检查如果文件存在于您使用的路径fileExistsAtPath并记录输出:

if ([[NSFileManager defaultManager] fileExistsAtPath:myPath]) { 

    NSLog(@"file exists!"); 
} 

myPath将是您正在获取的路径的NSString。我认为你很可能没有正确地获取文件的路径。你确定这些图像在文档目录中吗?如果您正在使用模拟器,则可以始终使用查找器来查找应用沙盒中的文件和文件夹。尝试通过去

~/Library/Application Support/iPhone Simulator/x.x/Applications/ 

希望这会有所帮助。

+0

Thanx的答复.....即时通讯使用故事板和setImage是UIimage视图,我已经定义了... – Rox

+0

好的..嗯也许你需要仔细检查图像的路径..尝试使用'[[NSFileManager sharedManager] fileExistsAtPath:path isDirectory:NO]'来测试它。 – KDaker

+0

我如何检查?你能给我完整的代码吗? – Rox

使用此代码..

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *docDirectory = [sysPaths objectAtIndex:0]; 

NSString *imagePath=[docDirectory stringByAppendingPathComponent:@"your image-name"]; 

UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)]; 
myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];  
[self.view addSubView:myimage]; 
[myimage release]; 

可能,这将帮助你...

+0

不... ...不工作... – Rox

+0

你试图转换成数据然后设置图像? – Abhishek

+0

是的,我试过我们的代码,但没有帮助... – Rox

如果你的文档目录中使用的图像以下:

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.m3u8"]; 
    UIImage *image=[UIImage imageWithContentsOfFile:newPath]; 
+0

嘿我已经使用这个....但它不显示 – Rox

+0

从该路径获取NSData,然后使用NSData设置图像。 –

+0

我该怎么做?你可以给我的代码? – Rox

已使用路径文件目录但没有提供路径图像存储在那里..检查如下

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil]; 
setImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]]; 
+0

无法正常工作...... – Rox

+0

检查重新转向的路径:'[NSString stringWithFormat:@“%@/imageName.png”,[filePathsArray objectAtIndex:0]]是否与图像存储的路径相同? – rptwsthi

试试这个,

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir  error:nil]; 
NSString *str_imgpath = [NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]] 
imgView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:str_imgpath]];