如何在 iOS 中将图像从资源中分离出来
我正在我的应用程序中显示一组图像。当我从资源文件夹加载图像时,代码可以正常工作,如下所示:
- (void)viewDidLoad
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat: @"pic_a"]];
[array addObject:[NSString stringWithFormat: @"pic_b"]];
[array addObject:[NSString stringWithFormat: @"pic_c"]];
[array addObject:[NSString stringWithFormat: @"pic_d"]];
int index = arc4random() % [array count];
NSString *pictureName = [array objectAtIndex:index];
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];
if (img != nil) { // Image was loaded successfully.
[imageView setImage:img];
[imageView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
但是,如果我在“资源”组中创建一个“图像”组,并尝试从那里加载图像,则其中的图像该视图显示为空白。
[array addObject:[NSString stringWithFormat: @"images/pic_a"]];
[array addObject:[NSString stringWithFormat: @"images/pic_b"]];
[array addObject:[NSString stringWithFormat: @"images/pic_c"]];
[array addObject:[NSString stringWithFormat: @"images/pic_d"]];
我想将图像从 nib 文件和所有其他文件中分离出来。最好的方法是什么?
I'm displaying a set of images in my app. I've got the code working when I load the image from the Resources folder, like so:
- (void)viewDidLoad
{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSString stringWithFormat: @"pic_a"]];
[array addObject:[NSString stringWithFormat: @"pic_b"]];
[array addObject:[NSString stringWithFormat: @"pic_c"]];
[array addObject:[NSString stringWithFormat: @"pic_d"]];
int index = arc4random() % [array count];
NSString *pictureName = [array objectAtIndex:index];
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];
if (img != nil) { // Image was loaded successfully.
[imageView setImage:img];
[imageView setUserInteractionEnabled:NO];
[img release]; // Release the image now that we have a UIImageView that contains it.
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
However, if I create an "images" group within the "Resources" group, and put try to load the images from there, the image within the view shows up blank.
[array addObject:[NSString stringWithFormat: @"images/pic_a"]];
[array addObject:[NSString stringWithFormat: @"images/pic_b"]];
[array addObject:[NSString stringWithFormat: @"images/pic_c"]];
[array addObject:[NSString stringWithFormat: @"images/pic_d"]];
I'd like to separate the images out from the nib files and all the other cruft. What's the best way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
即使您将图像放在资源中的单独组中,您也可以通过调用名称来加载它们,例如使用这一行
代替这三行:
您仍然可以简单地填充数组
如果您同时拥有 jpg 和 png 文件,那么您应该将
.png
附加到文件名的末尾。不然的话,把它关掉就好了。Even if you have the images in a separate group within Resources, you can load them by calling the name, e.g. use this one line
in place of these three lines:
You will still fill the array simply by
If you have both jpg and png files, then you should append
.png
to the end of the file names. Otherwise, leaving it off is fine.尝试使用
imageNamed:
方法而不是imageWithContentsOfFile:
Try using the
imageNamed:
method instead ofimageWithContentsOfFile:
您需要创建一个名为 images 的物理文件夹(它应该具有蓝色而不是通常的黄色)
You need to create a physical folder called images (it should have a blue color instead of the usual yellow color)
请记住,在项目中添加组只是为了在 Xcode 中进行组织和查看。添加组不会改变图像将保存在主包中的事实。如果您尝试使用字符串中的图像组来查找它们,它将找不到它们。
您可以采取一些措施来实现此目的,但实际上您不需要这样做。
Remember that adding groups inside your project it is simply for organization and look within Xcode. You adding a group will not change the fact that the images will be saved in the main bundle. If you are trying to find them using the images group in the string it will not find them.
There are some things you can do to make this work, but in reality you don't need to.