2013-09-02 2 views
0

아직 주소록에서 사용자의 연락처 이미지를 가져 오려고했습니다. 사용자 이름을 검색 할 수 있지만 문제를 파악할 수 없습니다. 이미지.iOS 6의 주소록에서 연락처 이미지를 가져 오는 방법

이것은 내가 지금까지 시도한 것입니다.

-(void)fetchAddressBook 
    { 
    self.reterivedNamesMutableArray =[[NSMutableArray alloc]init]; 
    ABAddressBookRef UsersAddressBook = ABAddressBookCreateWithOptions(NULL, NULL); 

    //contains details for all the contacts 
    CFArrayRef ContactInfoArray = ABAddressBookCopyArrayOfAllPeople(UsersAddressBook); 

    //get the total number of count of the users contact 
    CFIndex numberofPeople = CFArrayGetCount(ContactInfoArray); 

    UIImage* image; 
    //iterate through each record and add the value in the array 
    for (int i =0; i<numberofPeople; i++) 
    { 
    ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i); 
    ABMultiValueRef firstName = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty)); 
    ABMultiValueRef lastName = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty)); 


    NSString *tempFirstName = (__bridge NSString *)(firstName); 
    NSString *tempLastName = (__bridge NSString *)(lastName); 

    //Compose full name 
    NSString *fullName = @""; 

    if (firstName != nil) 
    { 
       fullName = [fullName stringByAppendingString:tempFirstName]; 
    } 

    if (lastName != nil) 
    { 
        fullName = [fullName stringByAppendingString:@" "]; 
        fullName = [fullName stringByAppendingString:tempLastName]; 
    } 

    if (ABPersonHasImageData(ref)) 
    { 
     image = [UIImage imageWithData:(__bridge NSData *)(ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail))]; 
    } 
    else 
    { 
     image = [UIImage imageNamed:@"default.png"]; 
    } 
    [self.reterivedNamesMutableArray addObject:fullName];  //Array of full name. 
    [self.reterivedImagesArray addObject:image];    //Array of contact images. 
    NSLog(@"Names array content = %@", self.reterivedNamesMutableArray); 
    NSLog(@"Images array content = %@", [self.reterivedImagesArray lastObject]);//This shows null 

} 
+0

는 당신이 self.reterivedImagesArray 초기화 + 할당 된 좋아하지을 ? –

+0

예, 이미 그랬습니다 ...... 시뮬레이터에서 프로젝트를 삭제하고 다시 시작했습니다 .... 그리고 지금은 작동하는 것 같습니다 ... – icodes

+0

코드에 많은 메모리 누수가 있습니다. "Create"또는 "Copy"함수에서 반환 된 객체를'CFRelease()'하거나 적절한 위치에서'__brigde_transfer' 나'CFBridgingRelease()'를 사용해야합니다. Xcode에서 "제품 -> 분석"을 실행하고 메모리 관리 지침을 읽는 것이 좋습니다. –

답변

0

당신이 코드 줄 사용하여 이미지를 얻을 수 있습니다 : - 말한다

if (ABPersonHasImageData(ref)) 
    { 
     NSData *imgData = (__bridge NSData *) ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail); 
     image = [UIImage imageWithData:imgData]; 
    } 
    else 
    { 
     image = [UIImage imageNamed:@"default.png"]; 
    } 

확인 Martin Rself.reterivedImagesArray가 할당되어 나

self.reterivedImagesArray=[[NSMutableArray alloc]init] 
+0

나는 똑같은 짓을 했어........하지만 NSLog는 이미지를 저장하는 배열을 가리키면 null이다. – icodes

+0

마틴은 배열에 * self.reterivedImagesArray *를 할당했다고 말합니다. self.reterivedImagesArray = [[NSMutableArray alloc] init] –