我在文檔目錄中有名爲「2」的文件夾。現在在文件夾「2」中,我有五個圖像,名爲0.png,1.png,2.png,3.png和4.png。我想要檢索這些圖像並保存到數組中。我有代碼,但它只返回一個圖像。從文檔目錄檢索多個圖像
int b=2;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d/0.png",b]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
NSLog(@"%@",getImagePath);
imge.image=img;
順便說一下,一般應該謹慎加載滿載圖像的陣列,因爲圖像通常會消耗大量內存。如果你只有幾個,或者如果你有一個引人注目的商業案例來加載它們,那沒問題。但十次中的九次,最好加載一組圖像名稱,並讓UI在需要時加載圖像,而不是提前加載所有圖像。 – Rob