ios 来自文件问题的 UIImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5811654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:43:47  来源:igfitidea点击:

UIImage from file problem

iphonexcodeiosuiimage

提问by Phil

I am trying to load an saved image but when I check the UIImage it comes back as nil. Here is the code:

我正在尝试加载保存的图像,但是当我检查 UIImage 时,它​​返回为零。这是代码:

UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"/var/mobile/Applications/B74FDA2B-5B8C-40AC-863C-4030AA85534B/Documents/70.jpg" ofType:nil]];

I then check img to see if it is nil and it is. Listing the directory shows the file, what am I doing wrong?

然后我检查 img 以查看它是否为 nil 并且是。列出目录显示文件,我做错了什么?

回答by WrightsCS

You need to point to the Documents directory within your app then like this:

您需要指向应用程序中的 Documents 目录,然后像这样:

- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

Usage:

用法:

UIImage *img = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/70.jpg",[self applicationDocumentsDirectory]]];

回答by Robin

First, you are using pathForResource wrong, the correct way would be:

首先,您使用 pathForResource 是错误的,正确的方法是:

[[NSBundle mainBundle] pathForResource:@"70" ofType:@"jpg"]

The whole idea of bundling is to abstract the resource path such as that it will always be valid, no matter where in the system your app resides. But if all you want to do is load that image I would recommend you use imageNamed: since it automatically handles retina resolution (high resolution) display detection on the iPhone for you and loads the appropriate resource "automagically":

捆绑的整个想法是抽象资源路径,例如无论您的应用程序驻留在系统中的哪个位置,它都将始终有效。但是,如果您只想加载该图像,我建议您使用 imageNamed: 因为它会自动处理 iPhone 上的视网膜分辨率(高分辨率)显示检测并“自动”加载适当的资源:

UIImage *img = [UIImage imageNamed:@"70.jpg"];

To easily support regular and retina resolution you would need to have two resources in your app bundle, 70.jpg and [email protected] with the @2x resource having both doubled with and height.

要轻松支持常规分辨率和视网膜分辨率,您的应用程序包中需要有两个资源,70.jpg 和 [email protected],@2x 资源同时具有和高度的两倍。

回答by Julian

Try loading a UIImage with:

尝试加载一个 UIImage :

[UIImage imageNamed:@"something.png"]

It looks for an image with the specified name in the application's main bundle. Also nice: It automatically chooses the Retina ([email protected]) or non-Retina (xyz.png) version.

它在应用程序的主包中查找具有指定名称的图像。也不错:它会自动选择 Retina ([email protected]) 或非 Retina (xyz.png) 版本。

回答by WrightsCS

Your path simply wont work because your app is in a sandbox, and you are trying to use the full path.

您的路径根本不起作用,因为您的应用程序在沙箱中,而您正尝试使用完整路径。

You should be using the following instead:

您应该改用以下内容:

UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"70" ofType:@"jpg"]];

UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"70" ofType:@"jpg"]];

or you can use, but is slower than the above:

或者你可以使用,但比上面慢:

UIImage *img = [UIImage imageNamed:@"70.jpg"];

UIImage *img = [UIImage imageNamed:@"70.jpg"];