如何获取图像的路径形式主要资源的资源 [英] How to get path of image form Resource of main bundle

查看:71
本文介绍了如何获取图像的路径形式主要资源的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPhone应用程序,其中我将 images 目录(组)添加到资源(组)。我想访问 images 中的图像。我需要一个指向该图像的路径,所以我创建了一个如下代码。

i have an iPhone application in which i added images directory (Group) in to Resources (group). i want to access that images which is in images. I need a path to that images so i create a code like following.

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage1" ofType:@"png" inDirectory:@"images"];
NSLog(@"%@", imagePath);

结果:

(null)

我也试过这段代码

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"myImage1" ofType:@"png"];

但是这段代码给了我(null)结果。

But this code gives me (null) result.

myImage1 的路径是 Resources / images / myImage1.png

我不知道是什么问题。请提出一些修正建议。

I don't know what is the problem. Please suggest some fixes.

推荐答案

通过以下方法添加图片解决了我的问题。

Adding image by following method solved my problem.


  • 右键单击xcode中的项目并选择将文件添加到项目名称

  • 从计算机本地磁盘中选择您的图像文件。

  • 在弹出窗口中,确保选择将项目复制到目标组的文件夹为任何添加的文件夹创建文件夹引用添加到目标

  • 单击添加

  • Right click on project in xcode and select Add Files to "project name".
  • Select your image file from computer local disk.
  • In the pop-up window make sure you select Copy items into destination group's folder and Create Folder References for any added folders and Add to targets.
  • click Add.

然后使用波纹管代码我可以检索我的图像。

Then with bellow code i can retrieve my image.

Objective-C 版本

[[NSBundle mainBundle] pathForResource:@"imageName" ofType:@"png"];

Swift 4.0 版本

Bundle.main.path(forResource: "imageName", ofType: "png")

这篇关于如何获取图像的路径形式主要资源的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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