如何显示图像从文档目录到图像视图?
问题描述:
我想从文档目录显示我的图像...我得到了目录的内容。 但是当我想显示图像,它什么也没有显示...我检查了文档目录.. 有图像..但不知道它为什么不显示在UIimage视图?如何显示图像从文档目录到图像视图?
任何人都可以帮我吗?哪里有问题???
我的代码
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[filePathsArray objectAtIndex:0]];
嘿,我得到的答案.....请点击此链接
感谢名单大家的宝贵帮助.......
答
我觉得你很困惑UIImage
与UIImageView
。创建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/
希望这会有所帮助。
答
使用此代码..
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];
可能,这将帮助你...
答
如果你的文档目录中使用的图像以下:
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.m3u8"];
UIImage *image=[UIImage imageWithContentsOfFile:newPath];
答
已使用路径文件目录但没有提供路径图像存储在那里..检查如下
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]];
答
试试这个,
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]];
所有在NSDocumentDirectory的形象呢? – Bazinga
你在newDir中传递了什么? –
@ Kobe.o4是的所有图像都在文档目录中...... – Rox