在 iOS 中以编程方式加载图像
在我的 iPad 应用程序中,我尝试以编程方式加载图像文件。该文件不是我的项目的一部分,因此 XCode 中没有引用它。 XCode 的“组和文件”列中没有该文件的条目。
图像必须在运行时加载,它的名称是从属性列表中读取的。
我尝试像这样加载文件:
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"png" inDirectory:@"MyDirectory"];
UIImage* retVal = [UIImage imageWithContentsOfFile:pathToImageFile];
在这种情况下,mydirectory
位于主包中,如下所示:
MyAmazingApp.app/MyDirectory/MyImage.png
不幸的是,图像将无法加载。我无法将图像文件添加到我的项目中,因为它的名称是在运行时确定的并且无法提前知道。文件名是在运行时从配置文件(属性列表)中读取的。
我做错了什么?我们将非常感谢您的帮助。
In my App for the iPad I'm trying to load an image file programmatically. The file is NOT part of my project, hence it is not referenced in XCode. There is no entry for the file in XCode's Groups and Files column.
The image has to be loaded at runtime instead, it name being read from a Property List.
I'm trying to load the file like this:
NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"png" inDirectory:@"MyDirectory"];
UIImage* retVal = [UIImage imageWithContentsOfFile:pathToImageFile];
In this case, mydirectory
lives in the main bundle like this:
MyAmazingApp.app/MyDirectory/MyImage.png
Unfortunately, the Image will not load. I can't add the image file to my project, as its name is to be determined at runtime and cannot be known in advance. The file name is read from a config file (a Property List) at runtime instead.
What am I doing wrong? Your help will be very much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 NSFileManager 获取一个或多个目录的内容。显然,您必须了解有关要加载的文件的一些信息,以便识别它,但是您可以使用 NSFileManager 来帮助您在应用程序包和应用程序的应用程序包中生成图像列表。文档目录。
You can use NSFileManager to get the contents of a directory, or several directories. You obviously have to know something about the file you want to load so that you can identify it, but you could for example use NSFileManager to help you generate a list of images in your app bundle and in your app's Documents directory.