2

In my application i asked user to draw sign and then i show that image in UIImageView using below Code:

UIGraphicsBeginImageContext(captureView.bounds.size);

[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
[ivStudentSign setImage:viewImage];
UIGraphicsEndImageContext();

NSMutableDictionary *tempDict=[[NSMutableDictionary alloc] init];
[tempDict setObject:UIImagePNGRepresentation(viewImage) forKey:userID];
[arrStoreSigns addObject:tempDict];
[userDef setObject:arrStoreSigns forKey:@"storeSigns"];

it almost works fine but sometime i get

setObjectForKey: object cannot be nil

that makes my application crash.What i'm doing wrong?I'm running application on ios 8.4.1

Viraj Padsala
  • 1,388
  • 1
  • 13
  • 31

3 Answers3

4

To figure this out, you need to go backward.

The object is nil, this means arrStoreSigns is nil. And arrStoreSigns is nil because tempDict is nil. tempDict is nil since UIImagePNGRepresentation(viewImage) doesn't give you any value, hence it become nil. This may happen since viewImage is nil due to UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); returning a nil image.

You put breakpoints and check, which of them above is actually failing.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Thanks for the answer, can you point to the documentation which says NSMutableDIctionary cannot have nil object – A. K. Dec 01 '17 at 07:18
  • 1
    "Neither a key nor a value can be nil" here https://developer.apple.com/documentation/foundation/nsdictionary?language=objc – Anoop Vaidya Dec 04 '17 at 14:34
0

The answer is right there in the crash log. setObjectForKey method requires an object which is not nil to be set to the NSDictionary. Check whether you are getting required values for UIImagePNGRepresentation(viewImage) or arrStoreSigns

Arun
  • 1,391
  • 1
  • 10
  • 29
0

In NSMutableDictionary, one can't set nil value for a key. It can be a NULL object. To ensure that nil and NULL object must not insert into dictionary, you can use following code snippet to avoid crash.

if (anObject != [NSNull null] && anObject != nil) {
     [self setObject:anObject forKey:aKey];
}

here anObject can be your image object and self can be your tempDict.

Your code:

UIGraphicsBeginImageContext(captureView.bounds.size);

[captureView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
[ivStudentSign setImage:viewImage];
UIGraphicsEndImageContext();

NSMutableDictionary *tempDict=[[NSMutableDictionary alloc] init];
if (viewImage != [NSNull null] && viewImage != nil) {
    [tempDict setObject:UIImagePNGRepresentation(viewImage)   forKey:userID];
    [arrStoreSigns addObject:tempDict];
    [userDef setObject:arrStoreSigns forKey:@"storeSigns"];
}
Sandeep Ahuja
  • 931
  • 6
  • 17
  • Thanks for the answer, can you point to the documentation which says NSMutableDIctionary cannot have nil object – A. K. Dec 01 '17 at 07:18