【问题标题】:How can i display images from document directory to image view?如何将图像从文档目录显示到图像视图?
【发布时间】:2012-07-03 13:29:37
【问题描述】:

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

谁能帮帮我???问题出在哪里???

我的代码

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

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

here

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

【问题讨论】:

  • NSDocumentDirectory 中的所有图片?
  • 你在 newDir 中传递了什么?
  • @Kobe.o4 是的,所有图片都在文档目录中...
  • @Jennis newDir 是我传递存储图像的目录路径的 NSString
  • 确保 newDir 值不为零。其余代码工作正常。

标签: iphone objective-c ios5 uiimageview


【解决方案1】:

使用此代码..

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];

希望对你有帮助...

【讨论】:

  • 请检查您在文档目录中的图像并复制路径并检查它是否为您提供了正确的图像...您是否使用正确的扩展名保存了此图像..?
【解决方案2】:

如果您在文档目录中的图片使用以下内容:

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

【讨论】:

  • 从该路径获取 NSData。然后使用 NSData 设置图像。
  • 试试这个:NSData *data=[NSData dataWithContentsOfFile:newPath] 只检查数据是否来了?
【解决方案3】:

试试这个,

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]];

【讨论】:

    【解决方案4】:

    我认为您将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/
    

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回复.....我正在使用情节提要,setImage 是 UIimage 视图,我已经定义了...
    • ok.. 嗯,也许您需要仔细检查图像的路径.. 尝试使用[[NSFileManager sharedManager] fileExistsAtPath:path isDirectory:NO] 进行测试。
    • 嘿 KDakerthanx 的帮助......我得到了我的解决方案......并更新了我的问题......
    【解决方案5】:

    您已使用文档目录的路径,但未提供存储在那里的图像的路径.. 请在此处查看以下内容

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

    【讨论】:

    • 查看返回的路径:[NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]是不是和你的图片存储的路径一样?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2013-02-24
    • 1970-01-01
    • 2015-08-27
    • 2012-09-04
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多