Forrest
Forrest

Reputation: 127873

How to save image to iphone's AddressBook using SDK API?

My purpose is to process one image from image library, and save this image directly to one person record (i,e, "Tom" ), which is selected from AddressBook, I can see the new image replace Tom's previous image, but I can not save it to AddressBook.

I implemented delegate ABPeoplePickerNavigationControllerDelegate, and using following logics, but whatever I tried, modified records can not be saved to address books.

Any clues or suggest ? Thanks

    ABAddressBookRef addressBook= ABAddressBookCreate();
    CFErrorRef error = NULL;
    BOOL wantToSaveChanges = YES;

    ABRecordSetValue(person, kABPersonFirstNameProperty, @"Shi", &error);//, &error)

    NSData * dataRef = UIImagePNGRepresentation(cellImage);
    ABPersonSetImageData(person, dataRef, &error);

    if (ABAddressBookHasUnsavedChanges(addressBook)) {\
        NSLog(@"need to save ");
        if (wantToSaveChanges) {
            ABAddressBookSave(addressBook, &error);
        } else {
            ABAddressBookRevert(addressBook);
        }
    }else {
        NSLog(@"no changes");
    }

    if (error != NULL) {/*... Handle error. ...*/
        NSLog(@"error happened here " );

    }

    CFRelease(addressBook);

Upvotes: 1

Views: 1198

Answers (1)

Related Questions