1

我正在尝试使用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 时,应用程序运行良好,并创建一个仅包含名称的新地址簿条目。

知道这里发生了什么吗?

4

1 回答 1

1

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

ABPersonSetImageData (
   ABRecordRef person,
   CFDataRef imageData,
   CFErrorRef *error
);
于 2013-05-14T06:19:21.943 回答