以下代码适用于模拟器,但崩溃了设备,因为img为nil:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
for (NSString* path in userImages) {
NSLog(@"image at %@", path); //logs "image at userImage0.png"
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:path];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
unichar ch = [path characterAtIndex:9];
NSLog(@"Ch = %c", ch); //correctly logs "0"
if (ch == '0') {
[self.images insertObject:img atIndex:0]; //this throws the error
}
}
为什么在设备上运行而不是在模拟器上运行时是img nil?图像最初保存如下:
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *imageName = @"userimage0.png";
NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];
[UIImagePNGRepresentation(img) writeToFile:imagePath atomically:YES];
答案 0 :(得分:0)
抱歉,这是大写问题。 userimage vs userImage。模拟器并不关心(我希望它能做到),但设备确实如此。