使用ABUnknownPersonViewController与图像崩溃

时间:2013-05-14 05:59:34

标签: ios objective-c uiimage addressbook

我正在尝试使用 ABUnknownPersonViewController 创建一个带有名称和图像的新地址簿条目,以提示是应将信息添加到现有人员还是添加为新人员。 根据地址簿编程指南和这里的一些帖子,我使用以下代码:

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];

当单步执行代码时,它显示dataRef已使用大约1.8MB的图像数据正确初始化。但是,在运行此代码时,应用程序崩溃并出现以下错误:

-[NSConcreteMutableData count]: unrecognized selector sent to instance 0x77f7ce0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData count]: unrecognized selector sent to instance 0x77f7ce0'

有问题的对象是imageData对象。

当我省略imageData时,应用程序运行良好并创建一个只有名称的新地址簿条目。

知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:1)

从图像制作NSData后,尝试使用此功能设置图片。

ABPersonSetImageData (
   ABRecordRef person,
   CFDataRef imageData,
   CFErrorRef *error
);