从iOS中具有文件夹名称的文件夹中检索图像

时间:2013-06-04 05:17:54

标签: ios path uiimage

我的所有图像都在文件夹名称ImageMe中,我喜欢使用此名称检索图像,如

[UIImage imageNamed:@"ImageMe/bulk-female.jpg"];

我是如何得到这种方法的,因为我从数据库中重新获取路径名,并且以这种格式即将到来。

4 个答案:

答案 0 :(得分:0)

如果您不想修改这些图像,只需在支持文件下创建文件夹名称“Resource”,然后将ImageMe文件夹保存在那里。然后使用下面的代码获取图像文件夹的路径。

NSString *theResourcePath = [NSBundle mainBundle] resourcePath];
NSString *theFilePath = [theResourcePath stringByAppendingPathComponent:@"ImageMe/bulk-female.jpg"];

答案 1 :(得分:0)

如果您在主捆绑中有图像,则可以使用它们 [UIImage imageNamed:@"theNameOfTheImage_WithOutDirecotry"];

您只需指定图像名称而不是目录。这是因为当应用程序打包在.ipa中时,只复制图像(复制包资源中的图像)而不是目录。

如果您要将图片打包到一个文件夹中,则必须创建新的捆绑包但不能使用imageNamed,您必须使用initWithContentsOfFile:(NSString *)path

以下是一些文档UIImageNSBundleBundle Programming Guide

答案 2 :(得分:0)

如果您的所有图像都以捆绑形式存在,那么您可以直接从名称中获取该图像。

像这样

[UIImage imageNamed:@"1.jpg"];

[UIImage imageNamed:@"2.jpg"]

[UIImage imageNamed:@"3.jpg"]

使用他们的名字直接获取图像。

如果您想将图片存放到特定的文件夹中。

NSString *Path = [NSBundle mainBundle] resourcePath];
NSString *FilePath = [Path stringByAppendingPathComponent:@"ImageMe/bulk-female.jpg"];

试试这个可能对你有所帮助。

答案 3 :(得分:0)

将您的图像添加到floder&然后将该文件夹添加到您的项目中

As shown in image, please select the option, Create folder references for any added folder

将文件夹添加到项目后,您可以使用以下代码访问图像。

NSString *bundlePath = [NSBundle mainBundle] bundlePath];
NSString *imagePath = [bundlePath stringByAppendingPathComponent:@"ImageFolder/imagename.jpg"];

希望这能解决您的问题