捆绑和文件访问 [英] Bundles and file access

查看:100
本文介绍了捆绑和文件访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中添加了一系列文件夹(使用添加现有文件选项)。这导致文件夹内容在Xcode中显示为蓝色文件夹。

I've adding a series of folders to my project (using the Add existing file option). This results in the folder contents being shown in Xcode as a blue folder.

以下作品:

NSString *imageName = [NSString stringWithFormat:@"/File-03/images/image-%02d.jpg", imageIndex];

return [UIImage imageNamed:imageName];

但是,我想切换到使用 imageWithContentsOfFile 因此图像不会被缓存。

However, I want to switch to using imageWithContentsOfFile so the images are not cached.

下面的代码为图像返回 nil

The code below returns nil for the image:

return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"/File-03/images/image-%02d.jpg", imageIndex]];

我还尝试使用a访问 imageWithContentsOfFile 捆绑,不去。

I also tried accessing the imageWithContentsOfFile using a bundle, no go.

我在这里缺少什么?

推荐答案

好的,我在代码中找到了一种方法来获得我需要的东西:

Okay, I figured out one way in code to get what I needed:

  NSString *dir = [NSString stringWithFormat:@"File-03/images", imageIndex];
  NSString *file = [NSString stringWithFormat:@"image-%02d", imageIndex]; 
  NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:@"jpg" inDirectory:dir];

  return = [UIImage imageWithContentsOfFile:path];

诀窍在于捆绑调用中的 inDirectory

The trick was to inDirectory in the bundle call.

这篇关于捆绑和文件访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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