简体   繁体   English

imageWithContentsOfFile不返回图像

[英]imageWithContentsOfFile Not Returning Image

UIImage *image=[UIImage imageWithContentsOfFile:filePath]; UIImage * image = [UIImage imageWithContentsOfFile:filePath];

I have used this method a hundred times before and it used to always work, but now it just returns" Null", I googled and I tried with 我以前曾经使用过这种方法一百次而且以前常常工作,但是现在它只返回“Null”,我用Google搜索并试着用

            NSBundle *bundle = [NSBundle mainBundle];
            NSString *filePath = [bundle pathForResource:
                                   @"fileName" ofType:@"png"];
            UIImage *image=[UIImage imageWithContentsOfFile:filePath];

But its still not Working, Also tried using 但它仍然没有工作,也尝试使用

UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]]; UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfFile:filePath]];

still get "Null", 仍然得到“空”,

And I can't use the Method 我不能使用该方法

  UIImage *image=[UIImage imageNamed:@"image.png"];

Since I am downloading the Images inside my Apps Document Directory inside a "tmp" folder and then using the path where I have downloaded them and then trying to access it, Hence I need this method "imageWithContentsOfFile:" to work desperately 由于我在“tmp”文件夹中的Apps文档目录中下载图像,然后使用我下载它们的路径然后尝试访问它,因此我需要这个方法“imageWithContentsOfFile:”拼命地工作

can somebody please help me out of this…Thanks for help in advance 有人可以帮我解决这个问题...提前感谢您的帮助

Not sure if the issue I was having is the same as yours but we were suddenly having this issue where we were getting nil for our images when using the full file path and using the imageWithContentsOfFilePath method. 不确定我遇到的问题是否与您的问题相同但我们突然遇到这个问题,在使用完整文件路径和使用imageWithContentsOfFilePath方法时,我们的图像为零。

We found after a bit of debugging that the NSHomeDirectory() (which is basically your sandbox location), now changes on every launch. 经过一些调试后我们发现NSHomeDirectory()(基本上是你的沙箱位置)现在每次启动都会改变。 Our issue was that we were storing the entire file path and on subsequent launches, trying to retrieve them from that same full file path, which no longer existed. 我们的问题是我们存储整个文件路径并在后续启动时尝试从同一个完整文件路径中检索它们,这些路径已不复存在。

For example, on one launch your home directory might be: /var/mobile/Containers/Data/Application/844B1DDA-49AB-4B5C-AC76-2BBF1397B142 but it would be different on the next: /var/mobile/Containers/Data/Application/C620B610-3839-4134-9DF7-C5756F22BBCE. 例如,在一次启动时,您的主目录可能是:/ var / mobile / Containers / Data / Application / 844B1DDA-49AB-4B5C-AC76-2BBF1397B142,但下一个会有所不同:/ var / mobile / Containers / Data /应用程序/ C620B610-3839-4134-9DF7-C5756F22BBCE。 Therefore, if you store file paths for future use, you'll have to determine the home directory portion of the file path at the time you require it and simply store the relative path of the file/image. 因此,如果存储文件路径以供将来使用,则必须在需要时确定文件路径的主目录部分,并只存储文件/映像的相对路径。

NSString *filePath = [NSString stringWithFormat:@"%@/%@", [self retrieveHomeDirectoryPath], relativeFileLocationPath];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];

Here retrieveHomeDirectoryPath would simply return the result of NSHomeDirectory() or NSTemporaryDirectory() and relativeFileLocationPath would be the path relative to that specific location. 这里retrieveHomeDirectoryPath只返回NSHomeDirectory()或NSTemporaryDirectory()的结果,relativeFileLocationPath将是相对于该特定位置的路径。

I would guess that there is something wrong with your import. 我猜你的导入有问题。 Xcode does funky things sometimes. Xcode有时会做一些时髦的事情。

Right click on the image in your resources and delete and remove the reference. 右键单击资源中的图像,然后删除并删除引用。 Reimport and make sure that you select the box that says copy. 重新导入并确保选择复制框。

This is how I set images. 这就是我设置图像的方式。

self.someUIImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"somePictureThatExists.png"]];

Or more simply: 或者更简单:

self.someUIImageView = [UIImage imageNamed:@"somePictureThatExists.png"];

Edit: 编辑:

I would also clean the project when you run into issues like this. 当你遇到这样的问题时,我也会清理项目。

Command+Shift+K or Product>Clean Command + Shift + K或Product>Clean

As you have to load image from Documents Directory 1st access it using below code 由于您必须从Documents Directory 1st加载图像,因此请使用以下代码访问它

NSArray  *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 

    NSUserDomainMask, YES);

    NSString *documentsDir  = [documentPaths objectAtIndex:0];

    NSString  *pngfile = [documentsDir stringByAppendingPathComponent:@"fileName.png"];

    & then apply filename to imageview like

     imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:pngfile]];

Here the filename.png is the name of png file which you have saved in Applications Document directory. 这里filename.png是您在Applications Document目录中保存的png文件的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM