Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIImage源文件问题_Iphone_Xcode_Ios_Uiimage - Fatal编程技术网

Iphone UIImage源文件问题

Iphone UIImage源文件问题,iphone,xcode,ios,uiimage,Iphone,Xcode,Ios,Uiimage,我试图加载保存的图像,但当我检查UIImage时,它返回为nil。代码如下: UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"/var/mobile/Applications/B74FDA2B-5B8C-40AC-863C-4030AA85534B/Documents/70.jpg" ofType:nil]]; 然后我检查img,看它是否为零,是否为。列出目录会显示

我试图加载保存的图像,但当我检查UIImage时,它返回为nil。代码如下:

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

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

尝试加载带有以下内容的UIImage:

[UIImage imageNamed:@"something.png"]

它在应用程序的主捆绑包中查找具有指定名称的映像。也很好:它自动选择视网膜(xyz@2x.png)或者非视网膜(xyz.png)版本。

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

[[NSBundle mainBundle] pathForResource:@"70" ofType:@"jpg"]
捆绑的整个思想是抽象资源路径,使其始终有效,无论应用程序驻留在系统中的何处。但是,如果您只想加载该图像,我建议您使用ImageName:因为它可以自动处理iPhone上的视网膜分辨率(高分辨率)显示检测,并“自动”加载相应的资源:


要轻松支持常规分辨率和视网膜分辨率,您的应用程序包中需要有两个资源:70.jpg和70@2x.jpg由于@2x资源的长度和高度都增加了一倍。

您的路径根本无法工作,因为您的应用程序位于沙箱中,并且您正在尝试使用完整路径

您应该使用以下选项:

UIImage*img=[UIImage-imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“70”of type:@“jpg”]

或者您可以使用,但速度比上面的慢:

UIImage*img=[UIImage-imagename:@“70.jpg”]


您需要指向应用程序中的文档目录,然后如下所示:

- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}
用法:
感谢您的回复,我尝试了这两种方法,但仍然得到了零:if(img==nil){NSLog(@“is nil”);}我开始怀疑是否保存错误。我可以在我的文档目录中看到“70.jpg”存在。这就是你的意思吗,我对“你的包裹”有点困惑。再次感谢。请检查项目文件,也可以打开.app文件以确保资源已被复制。很可能该文件未包含在您的项目中,如果包含,则我提供的示例将起作用。我正在从相机捕获图像并将其保存到文档目录中。您的解决方案的问题是,
UIImage
正在应用包中查找jpg,他从应用程序的文档目录中需要它。我的视网膜图像有问题,这解决了我的问题。非常感谢。
- (NSString *)applicationDocumentsDirectory
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}
UIImage *img = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/70.jpg",[self applicationDocumentsDirectory]]];