开发者

How to separate images out from Resources in iOS

开发者 https://www.devze.com 2023-04-06 18:36 出处:网络
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:

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];

开发者_如何学C    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?


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.


Try using the imageNamed: method instead of imageWithContentsOfFile:


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号