【问题标题】:IOS get image from document folder use full linkIOS从文件夹中获取图像使用完整链接
【发布时间】:2016-03-04 04:20:39
【问题描述】:

我已经将一些图像保存到文件夹中,之后,我使用链接完整来获取图像。

strImageName 是图片的链接完整路径。

    +(UIImageView *)getImage:(NSString*)strImageName showImage:(UIImageView*)showImage{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:strImageName]];
    UIImage *image = [UIImage imageWithData:data];
    showImage.image = image;
    return showImage;
}

但是,它的 get data = nil。

当我登录时,它会显示:

Failed to read file, error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x8517ff0 {NSFilePath=/var/mobile/Containers/Data/Application/7EE5BEC1-0F60-461A-8B29-5F9B425ED1A6/Documents/emoji29.png, NSUnderlyingError=0x8517f70 "The operation couldn’t be completed. No such file or directory"}

我研究了一些方法,但对我不起作用。

我该如何解决这个问题。

【问题讨论】:

  • 您是否尝试将图像名称附加到文档目录?当您重新安装应用程序时,应用程序文件夹有时会更改其名称。 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *docDirectory = [paths objectAtIndex:0];NSString *imageName = [docDirectory stringByAppendingString:@"/imageName"];
  • 使用 dataWithContentsOfURL:options:error: 方法找出它返回 nil 的原因。错误将在作为方法参数传递的 NSError* 指针中返回。
  • 如何解决这个问题,我使用完整链接到文件夹来获取图像:/var/mobile/Containers/Data/Application/B4A07DB8-FD73-461E-86EE-24CA8A3F7855/Documents/ emoji32.png

标签: ios objective-c uiimageview nsurl nsdocumentdirectory


【解决方案1】:

NSString *filePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"] isDirectory:NO];

UIImage *image = [UIImage imageWithContentsOfFile:filePath];

【讨论】:

  • 在您的答案中添加一些描述,使其更易于理解或对其他人有帮助