The following code works on the simulator but crashes the device because img is 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
}
}
Why is img nil when run on the device but not on the simulator? The image is originally saved like this:
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *imageName = @"userimage0.png";
NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];
[UIImagePNGRepresentation(img) writeToFile:imagePath atomically:YES];
Sorry, it was a capitalization problem. userimage vs userImage. The simulator doesn't care about that (I wish it did), but the device does.