我有一些代碼,它隨機顯示給定的.png圖像及其標題。標題邏輯工作正常。但是,由於某種原因,只顯示四個圖像中的兩個。爲了防萬一,我試過改變順序,但只有那兩張圖片pic_c和pic_d纔會顯示出來。我還檢查了拼寫,並且這些文件在資源中並存在於文件系統中。下面是顯示圖像的代碼:只有一些PNG圖像顯示,其他人沒有明顯的理由
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];
}
任何想法爲什麼會發生這種情況?
最近信息:如果我刪除titleArray,並僅使用文件名數組,則會顯示所有圖像。不過,我仍然希望能夠使用titleArray。
嘗試更改文件名,以便它們不包含_,只是猜測。 –