0
我的應用程序使用應用程序文檔文件夾中的.png圖像在稍後彈出時顯示縮略圖。我正在嘗試將文件夾中的所有.png圖像添加到數組中。我知道tempImage對象被添加到數組時發生錯誤,但我不確定這是爲什麼發生。有沒有人看到這個代碼塊的問題?無法將UIImage項目添加到NSMutableArray - EXC_BAD_ACCESS錯誤
NSArray *pathforsave = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [pathforsave objectAtIndex:0];
NSArray *getFiles = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentDirectory
error:nil];
NSInteger x = 0;
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *onlyPNGs = [getFiles filteredArrayUsingPredicate:fltr];
NSMutableArray *Images;
NSUInteger arrayPNGLength = [onlyPNGs count];
for (x=0; x<arrayPNGLength; x++) {
//first get the path
NSString *strFile = [documentDirectory stringByAppendingPathComponent:[onlyPNGs objectAtIndex:x]];
UIImage *tempImage = [ UIImage imageWithContentsOfFile: strFile];
[Images addObject:tempImage];
}
感謝您的幫助,我是新來的目標C。
安德烈是正確的,你需要'alloc' /'init'的'MSMutableArray'你使用它之前。但是,除此之外,Objective-C [印刷變量命名約定](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingBasics.html#//apple_ref/doc/uid/20001281-1002931-BBCFHEAB)是變量名稱(和方法名稱)應該以小寫字母開頭。只有類名以大寫字母開頭。所以'NSMutableArray * images = [[[[[[NSMutableArray alloc] init] autorelease];' – Rob