在UIImage imageNamed中使用外部图像 [英] Use external image in UIImage imageNamed

查看:65
本文介绍了在UIImage imageNamed中使用外部图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Web Apple.png和Apple@2x.png下载两个图像.我想使用 [UIImage imageNamed:@"Apple.png"] ,以便它可以使用内置功能来检测应显示Apple.png还是应显示Apple@2x.png.

I am downloading two images from the web Apple.png and Apple@2x.png. I want to use [UIImage imageNamed:@"Apple.png"] so it can use the build in features to detect whether it should display either Apple.png or Apple@2x.png.

现在如何将这些图像存储在哪里?我在文档中阅读了以下内容:

Now where do I store these images? I read the following in the documentation:

文件名.如果这是第一次加载图片时,该方法使用应用程序中指定的名称主捆绑包.

The name of the file. If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.

嗯,因此应用程序的主捆绑包是必经之路.这是我的代码:

Ah so the Application's Main Bundle is the way to go. This is what my code looks like:

NSString      *directory   = [[NSBundle mainBundle] bundlePath]; 
NSString      *path        = [directory stringByAppendingPathComponent:imageName];
NSFileManager *fileManager = [NSFileManager defaultManager];

[fileManager createFileAtPath:path contents:dataFetcher.receivedData attributes:nil];

我检查了该文件是否是在路径的文件夹中创建的,这是正确的.我还将Example.png拖到我的项目文件中,只是看它是否存储在同一文件夹中,而且这也是正确的.

I checked if the file was created in the folder of the path and that was correct. I also dragged an Example.png in my project file just to see if it got stored in the same folder and that also was correct.

但是, [UIImage imageNamed:@"Apple.png"] 仍无法获取图像.

However, [UIImage imageNamed:@"Apple.png"], still fails to fetch the images.

推荐答案

您不能对应用程序下载的图片使用 UIImage 'a + imageNamed:方法.该方法的确在应用程序包中查找图像,但是不允许您的应用程序在运行时更改其自己的包.

You cannot use UIImage'a +imageNamed: method with pictures downloaded by the app. That method looks for images in the app bundle indeed, but your app is not allowed to change its own bundle at run time.

这篇关于在UIImage imageNamed中使用外部图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆