Reputation: 13
I'm trying to create a new address book entry with a name and an image using the ABUnknownPersonViewController to prompt whether the information should be added to an existing person or added as a new person. According to the address book programming guide and a few postings here, I'm using the following code:
ABUnknownPersonViewController *view = [[ABUnknownPersonViewController alloc] init];
view.unknownPersonViewDelegate = self;
CFErrorRef error = NULL;
// Create a new person record
ABRecordRef person = ABPersonCreate();
ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(_nameTextField.text), &error);
ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(_descriptionTextField.text), &error);
//NSData *imageData = UIImageJPEGRepresentation(_image, 0.8);
NSData *imageData = UIImagePNGRepresentation(_image);
CFDataRef dataRef = (__bridge CFDataRef)imageData;
ABRecordSetValue(person, kABPersonImageFormatOriginalSize, dataRef, &error);
//
view.displayedPerson = person;
view.allowsAddingToAddressBook = YES;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:view];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(closeAddressBook)];
view.navigationItem.leftBarButtonItem = doneButton;
navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:navController animated:YES];
When stepping through the code, it shows that dataRef is initialized correctly with some 1.8MB image data. However when running this code, the app crashed with the following error:
-[NSConcreteMutableData count]: unrecognized selector sent to instance 0x77f7ce0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData count]: unrecognized selector sent to instance 0x77f7ce0'
The object in question is the imageData object.
When I leave out the imageData, the app runs fine and creates a new address book entry with just the name.
Any idea what's going on here?
Upvotes: 1
Views: 312
Reputation: 1313
Try this function to set picture after you make NSData from image.
ABPersonSetImageData (
ABRecordRef person,
CFDataRef imageData,
CFErrorRef *error
);
Upvotes: 1