【问题标题】:I am loading image from local from ios device but its not desplaying我正在从 ios 设备从本地加载图像,但它没有显示
【发布时间】:2017-01-07 17:09:57
【问题描述】:

我想在ios中从本地加载图片,

我在我的文件中添加了这段代码,

NSString *path = [NSString stringWithFormat:@"%@/Documents/Small-mario.png", NSHomeDirectory()];
    NSLog(@"path %@", path);
displayImg.image = [UIImage imageNamed:path];

iget 的路径是,

/Users/WTS-New/Library/Developer/CoreSimulator/Devices/F43C26D2-DCFF-4764-AAF0-F6CC7BCDCF5D/data/Containers/Data/Application/06FE9A96-7705-4FB7-B291-4716CC6679C3/Documents/Small- mario.png

但是图像没有显示在图像视图中。

【问题讨论】:

  • imageNamed 不起作用,使用 NSData dataWithContentsOfFile:path 然后 UIImage imageWithData:
  • 图片“Small-mario.png”在您的应用程序包中吗?
  • 他的图片“Small-mario.png”在我的画廊中。 @ddb
  • 画廊是什么意思?
  • 我的 iphone 画廊。 @KetanParmar

标签: ios objective-c xcode uiimageview


【解决方案1】:

在您的应用程序中,您无法访问不在您的应用程序包中的图像

你有两个选择:

  1. 使用 UIImagePickerController 导入它

  2. 将您需要的图像放入您的应用程序包中

【讨论】:

  • 你能举个例子吗? @ddb
  • 关于什么?请点击链接,他们将引导您查看 StackOverflow 文档;)
  • 你能描述一下如何将它存储在 app bundle 中吗? @ddb
  • 你必须把图像文件放到你Xcode项目的“图像资源”中,你可以看看怎么做at this link
【解决方案2】:

请用这个

NSString *path = [NSString stringWithFormat:@"%@/Documents/Small-mario.png", NSHomeDirectory()];
    NSLog(@"path %@", path);
displayImg.image = [UIImage imageWithContentsOfFile:path];

或者你可以使用

 NSURL *destinationURL = [NSURL fileURLWithPath:path];

displayImg.image =[UIImage imageWithData:[NSData dataWithContentsOfURL:destinationURL]]

【讨论】:

  • 我得到了类似的图像路径,“/Users/WTS-New/Library/Developer/CoreSimulator/Devices/F43C26D2-DCFF-4764-AAF0-F6CC7BCDCF5D/data/Containers/Data/Application/8E49D340 -7CAC-4031-85BF-9E5C26A1E37A/Documents/Small-mario.png",但图像未显示在 displayImg.image 中。 @balkaran singh
【解决方案3】:

你可以选择像,

 UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; // this is your picked image

[picker dismissViewControllerAnimated:YES completion:nil];
}

并确保您确认UIImagePickerControllerDelegateUINavigationControllerDelegate

【讨论】:

  • 你能描述一下如何将它存储在 app bundle 中吗? @Ketan Parmar
  • 您可以将其存储在文档目录中。您可以以编程方式将任何内容存储在应用程序包中。您可以在 xcode 中拖放应用程序包中的图像。您没有 app bundle 的写入权限,因此无法以编程方式编写。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
相关资源
最近更新 更多