如何在 iOS 中将图像从资源中分离出来

发布于 2024-12-05 13:51:23 字数 1459 浏览 2 评论 0原文

我正在我的应用程序中显示一组图像。当我从资源文件夹加载图像时,代码可以正常工作,如下所示:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小鸟爱天空丶 2024-12-12 13:51:23

即使您将图像放在资源中的单独组中,您也可以通过调用名称来加载它们,例如使用这一行

UIImage *img = [UIImage imageNamed:[array objectAtIndex:index]];

代替这三行:

NSString *pictureName = [array objectAtIndex:index];  
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];

您仍然可以简单地填充数组

[array addObject:[NSString stringWithFormat: @"pic_a"]];

如果您同时拥有 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

UIImage *img = [UIImage imageNamed:[array objectAtIndex:index]];

in place of these three lines:

NSString *pictureName = [array objectAtIndex:index];  
NSString* imagePath = [ [ NSBundle mainBundle] pathForResource:pictureName ofType:@"png"];
UIImage *img = [ UIImage imageWithContentsOfFile: imagePath];

You will still fill the array simply by

[array addObject:[NSString stringWithFormat: @"pic_a"]];

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.

三月梨花 2024-12-12 13:51:23

尝试使用 imageNamed: 方法而不是 imageWithContentsOfFile:

Try using the imageNamed: method instead of imageWithContentsOfFile:

花落人断肠 2024-12-12 13:51:23

您需要创建一个名为 images 的物理文件夹(它应该具有蓝色而不是通常的黄色)

You need to create a physical folder called images (it should have a blue color instead of the usual yellow color)

初相遇 2024-12-12 13:51:23

请记住,在项目中添加组只是为了在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文