-1

I'm loading images from my app's image.xcassets using [UIImage imageNamed:@"filename"] method and only the first image is loading correctly but then the rest of the images returns nil and crashes the app. Below is my code:

[self.imagePicker addImage:[UIImage imageNamed:@"image0-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image1-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image2-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image3-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image4-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image5-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image6-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image7-150x277"]];
[self.imagePicker addImage:[UIImage imageNamed:@"image8-150x277"]];

And here is my addImage method:

- (void)addImage:(UIImage *)image 
{
    [_images addObject:image];
    [_thumbs addObject:image];
}

I have checked this question and tried everything in it including file names check and making sure the images are Retina 2x but nothing worked. Any ideas on what might be wrong?

And here is the crash log:

2016-10-14 15:28:10.280 Munasabah[38773:1038198] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: ( 0 CoreFoundation 0x000000010d65134b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010d0b221e objc_exception_throw + 48 2 CoreFoundation 0x000000010d58238f -[__NSArrayM insertObject:atIndex:] + 1375 3 Munasabah 0x0000000108dcb669 -[CustomImagePicker addImage:] + 73 4 Munasabah 0x0000000108dc8012 -[FlipsideViewController viewDidLoad] + 994 5 UIKit 0x000000010b4cd06d -[UIViewController loadViewIfRequired] + 1258 6 UIKit 0x000000010b4cd4a0 -[UIViewController view] + 27 7 UIKit 0x000000010bd918d0 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87 8 UIKit 0x000000010b4a7d26 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 141 9 UIKit 0x000000010b4e030b -[UIViewController _presentViewController:withAnimationController:completion:] + 3956 10 UIKit 0x000000010b4e36df -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 530 11 UIKit 0x000000010b4e31c5 -[UIViewController presentViewController:animated:completion:] + 179 12 Munasabah 0x0000000108dc7632 -[MainViewController showInfo:] + 354 13 UIKit 0x000000010b32db88 -[UIApplication sendAction:to:from:forEvent:] + 83 14 UIKit 0x000000010b4b32b2 -[UIControl sendAction:to:forEvent:] + 67 15 UIKit 0x000000010b4b35cb -[UIControl _sendActionsForEvents:withEvent:] + 444 16 UIKit 0x000000010b4b24c7 -[UIControl touchesEnded:withEvent:] + 668 17 UIKit 0x000000010b39b0d5 -[UIWindow _sendTouchesForEvent:] + 2747 18 UIKit 0x000000010b39c7c3 -[UIWindow sendEvent:] + 4011 19 UIKit 0x000000010b349a33 -[UIApplication sendEvent:] + 371 20 UIKit 0x000000010bb3bb6d __dispatchPreprocessedEventFromEventQueue + 3248 21 UIKit 0x000000010bb34817 __handleEventQueue + 4879 22 CoreFoundation 0x000000010d5f6311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 23 CoreFoundation 0x000000010d5db59c __CFRunLoopDoSources0 + 556 24 CoreFoundation 0x000000010d5daa86 __CFRunLoopRun + 918 25 CoreFoundation 0x000000010d5da494 CFRunLoopRunSpecific + 420 26 GraphicsServices 0x000000010f712a6f GSEventRunModal + 161 27 UIKit 0x000000010b32bf34 UIApplicationMain + 159 28 Munasabah 0x0000000108dc430e main + 62 29 libdyld.dylib 0x000000010df2c68d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

And a screen shot for the target membership:

enter image description here

Community
  • 1
  • 1
Ali
  • 4,205
  • 4
  • 25
  • 40
  • Try storing images a temporary UIImage object and while debugging check if the object contains the image. (The eye icon shows you the preview of the image) – Rikh Oct 14 '16 at 12:29
  • @Rikh What do you mean by storing images a temporary UIImage object? – Ali Oct 14 '16 at 12:32
  • @Ali Please check if you have initialized your array in which you are saving the images. – Deepak Sharma Oct 14 '16 at 12:34
  • @DeepakSharma The array is initialized via `// Create and Initialize image picker self.imagePicker = [[CustomImagePicker alloc] init];` which is just before the image load in in the question. and I have the following in the `CustomIMagePicker` Code: `- (id) init { if ((self = [super init])) { _images = [[NSMutableArray alloc] init]; _thumbs = [[NSMutableArray alloc] init]; } return self; }` – Ali Oct 14 '16 at 12:37
  • Try UIImage *tempImage = [UIImage imageNamed:@""]; And when debugging, check if you can preview the image when you hover your mouse over the tempImage object. Or check if "tempImage" object is nil or not. Also make sure _images and _thumbs have both been initalized – Rikh Oct 14 '16 at 12:38
  • @Rikh I tried `UIImage *tempimage = [UIImage imageNamed:@"image1-150x277"];` and tempimage returns nil – Ali Oct 14 '16 at 12:43
  • @Ali Put same image in 1x and 3x as well and than try once – Deepak Sharma Oct 14 '16 at 12:52

1 Answers1

2

The image names in the screenshot appear to be different than the ones you are using in the code! In the code it has a "277" in the end where as in the image name it is a "227"

Rikh
  • 4,078
  • 3
  • 15
  • 35