[英]How can i display images from document directory to image view?
I want to display my images from document directory... i got the contents of directory. 我想显示文档目录中的图像...我得到了目录的内容。 but when i want to display image, it display nothing... I checked in document directory.. there are images.. but don't know why it is not displaying in UIimage view??? 但是当我想显示图像时,它什么也不显示...我在文档目录中签到了。
can anybody help me??? 有谁能够帮助我??? where is the problem??? 问题出在哪儿???
MY code 我的密码
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[filePathsArray objectAtIndex:0]];
Hey i got the answer..... Please follow this link 嘿,我得到了答案.....请点击此链接
Thanx to everyone for their precious help........ 感谢每个人的宝贵帮助........
I think you are confusing UIImage
with UIImageView
. 我认为您将UIImage
与UIImageView
混淆了。 After creating your UIImage
setImage, you need to set it to a UIImageView: 创建UIImage
setImage之后,需要将其设置为UIImageView:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:setImage];
then you need to add the myImageView to your view: 那么您需要将myImageView添加到您的视图中:
[self.view addSubview:myImageView];
EDIT : 编辑 :
to check for if the file exists at a path you use fileExistsAtPath
and log the output: 检查文件是否存在于您使用fileExistsAtPath
的路径并记录输出:
if ([[NSFileManager defaultManager] fileExistsAtPath:myPath]) {
NSLog(@"file exists!");
}
myPath would be the NSString for the path you are obtaining. myPath将是您要获取的路径的NSString。 I think most likely that you are not getting the path to the file correctly. 我认为您很可能没有正确获取文件的路径。 Are you sure the images are in the documents directory? 您确定图像在文档目录中吗? if you are using the simulator you can always use finder to locate the files and folders inside your app's sandbox. 如果您使用的是模拟器,则始终可以使用finder查找应用程序沙箱中的文件和文件夹。 try to look your images up manually by going to 尝试通过以下方法手动查找图像
~/Library/Application Support/iPhone Simulator/x.x/Applications/
hope this helps. 希望这可以帮助。
Use this code.. 使用此代码。
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];
may this will help you... 可以帮助您...
If your image in the document directory use following: 如果您的图像在文档目录中,请使用以下命令:
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.m3u8"];
UIImage *image=[UIImage imageWithContentsOfFile:newPath];
You have used path for document directory but not provided path for image stored there.. check following here 您已使用文档目录路径,但未提供存储图像的路径。
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]];
Try this, 尝试这个,
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.