,當我有這樣的代碼,列舉的地址簿中的聯繫人 -崩潰釋放ABAddressBookRef
+(void)enumerateAddressBookContacts:(void(^)(NSString *name, NSArray *phoneNumbers, NSArray *emailAddresses, NSData *imageData, NSString *recordId))enumerationBlock failure:(void (^)(NSError *error))failure{
[self requestAddressBookPermissionsWithCompletion:^{
if (enumerationBlock)
{
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
CFArrayRef addressBookPeople = ABAddressBookCopyArrayOfAllPeople (addressBook);
NSArray* people = (__bridge NSArray*)addressBookPeople;
if ([people count])
{
for (NSUInteger i = 0; i < [people count]; i++)
{
ABRecordRef person = (__bridge ABRecordRef)people[i];
NSArray *phoneNumbers = [self phoneNumbersOfPerson:person];
NSArray *emailAddresses = [self emailAddressesOfPerson:person];
NSString *fullName = [self fullNameOfPerson:person];
NSData *imageData = [self imageDataOfPerson:person];
NSString *recordId = [NSString stringWithFormat:@"%d", ABRecordGetRecordID(person)];
void (^block)(NSString*, NSArray *, NSArray *, NSData*, NSString*) = [enumerationBlock copy];
block(fullName,phoneNumbers,emailAddresses,imageData,recordId);
}
}
else
{
//No contacts in addressbook
if (failure) {
NSError *error = [NSError errorWithDomain:kAddressBook code:kAddressBookUploadFailReasonEmptyAddressBook userInfo:@{NSLocalizedDescriptionKey:kAddressBookUploadFailReason[kAddressBookUploadFailReasonEmptyAddressBook]}];
failure(error);
}
}
if (addressBookPeople)
{
CFRelease(addressBookPeople);
}
if (addressBook)
{
CFRelease (addressBook);
}
}
} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
我對ABAddressBookRef
釋放對象崩潰的方法結束。
這是枚舉法的一個示例(他們都採取同樣) -
+ (NSArray*)phoneNumbersOfPerson:(ABRecordRef)person
{
ABMutableMultiValueRef phones = ABRecordCopyValue (person, kABPersonPhoneProperty);
NSMutableArray *phoneNumbers = [NSMutableArray array];
CFIndex phonesNumberCount = ABMultiValueGetCount (phones);
if (phonesNumberCount > 0)
{
for (CFIndex index = 0; index < phonesNumberCount; index++)
{
CFStringRef phoneValue = ABMultiValueCopyValueAtIndex (phones, index);
[phoneNumbers addObject:(__bridge_transfer NSString *)phoneValue];
if (phoneValue)
{
CFRelease (phoneValue);
}
}
}
if (phones)
{
CFRelease (phones);
}
return phoneNumbers;
}
當我刪除釋放我不崩潰,但我想我會有泄漏。 任何想法可能是什麼原因?
感謝
用戶產品 - >分析,你可能會得到一個很好的線索。 – jamihash
謝謝,你是對的。看看我的答案 – shannoga