[英]How can i display images from document directory to image view?
我想显示文档目录中的图像...我得到了目录的内容。 但是当我想显示图像时,它什么也不显示...我在文档目录中签到了。
有谁能够帮助我??? 问题出在哪儿???
我的密码
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。 我认为您很可能没有正确获取文件的路径。 您确定图像在文档目录中吗? 如果您使用的是模拟器,则始终可以使用finder查找应用程序沙箱中的文件和文件夹。 尝试通过以下方法手动查找图像
~/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]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.