我正在嘗試保存firstName
和lastname
。 NSLog(firstName);
從地址簿輸出正確的值,但_dossier.firstName
爲空。 圖像保存正確。從CFStringRef保存NSString。 ARC
ABAddressBookRef addressBook = ABAddressBookCreate();
for (TKAddressBook *ab in contacts) {
NSNumber *personID = [NSNumber numberWithInt:ab.recordID];
ABRecordID abRecordID = (ABRecordID)[personID intValue];
ABRecordRef abPerson = ABAddressBookGetPersonWithRecordID(addressBook, abRecordID);
NSString* firstName = nil;
NSString* lastName = nil;
// Check person image
UIImage *personImage = nil;
if (abPerson != nil && ABPersonHasImageData(abPerson)) {
firstName = (__bridge NSString*)ABRecordCopyValue(abPerson,
kABPersonFirstNameProperty);
NSLog(firstName);
lastName = (__bridge NSString*)ABRecordCopyValue(abPerson,
kABPersonLastNameProperty);
CFDataRef contactThumbnailData = ABPersonCopyImageDataWithFormat(abPerson, kABPersonImageFormatThumbnail);
personImage = [UIImage imageWithData:(__bridge NSData*)contactThumbnailData];
CFRelease(contactThumbnailData);
[_document.managedObjectContext performBlock:^() {
Dossier *dossier = [NSEntityDescription insertNewObjectForEntityForName:@"Dossier"
inManagedObjectContext:_document.managedObjectContext];
_dossier.firstName = firstName;
_dossier.lastName = lastName;
dossier.photo = personImage;
}];
}
}
什麼是_dossier?不應該是'dossier.firstName = firstName; dossier.lastName = lastName;'? –
_dossier是UIManagedDocument – Shmidt
我不認爲這會對問題有所幫助,但是您應該對來自'ABRecordCopyValue'的返回值使用'__bridge_transfer'(或'CFBridgingRelease')。 –