I have some code which randomly displays a given .png image and its title.The title logic is working fine. However, for some reason, only two of the four images display. I’ve tried changing the order just in case, but only those two images, pic_c and pic_d, ever get displayed. I’ve also checked the spelling, and that the files are in resources and exist on the file system. Here’s the code which displays the image:
NSMutableArray *fileNameArray = [[NSMutableArray alloc] init];
[fileNameArray addObject:[NSString stringWithFormat: @"pic_a"]];
[fileNameArray addObject:[NSString stringWithFormat: @"pic_b"]];
[fileNameArray addObject:[NSString stringWithFormat: @"pic_c"]];
[fileNameArray addObject:[NSString stringWithFormat: @"pic_d"]];
NSMutableArray *titleArray = [[NSMutableArray alloc] init];
[titleArray addObject:[NSString stringWithFormat: @"pic a"]];
[titleArray addObject:[NSString stringWithFormat: @"pic b"]];
[titleArray addObject:[NSString stringWithFormat: @"pic c"]];
[titleArray addObject:[NSString stringWithFormat: @"pic d"]];
int index = arc4random() % [fileNameArray count];
NSString *pictureName = [titleArray objectAtIndex:index];
art_title.text = pictureName;
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];
[fileNameArray release];
[titleArray release];
}
Any idea why this might be happening?
Recent information: If I remove the titleArray, and use the file name array only, all the images show up. However, I’d still like to be able to use the titleArray.
What are your image files actually called? In the code above you don’t actually do anything with finleNameArray except count it. You use the values from titleArray to get the image resource. I would think you need the following code in there:
Then change your
imagePathto use this instead ofpictureName.