ios - Adding contact image to address book iPhone -


i developing application changes contact picture of contact when select contact contact picker .

here code have implemented far,

(bool)peoplepickernavigationcontroller:(abpeoplepickernavigationcontroller *)peoplepicker shouldcontinueafterselectingperson:(abrecordref)person {     cferrorref error=nil;     abaddressbookref addressbook = abaddressbookcreate();     nsstring *path1 = [[nsbundle mainbundle] pathforresource:@"birthday.jpg" oftype:nil];     uiimage *img = [uiimage imagewithcontentsoffile:path1];     nsdata *dataref = uiimagepngrepresentation(img);      cfdataref cfdataref = cfdatacreate(null, [dataref bytes], [dataref length]);     if(abpersonhasimagedata(person))    {        abpersonremoveimagedata(person, &error);        abaddressbooksave(addressbook, &error);    }     if (abpersonsetimagedata(person,cfdataref, &error))     {         nslog(@"set contact photo %@", error);          if (abaddressbookhasunsavedchanges(addressbook))         {             nslog(@"changes made address book");         }         else {             nslog(@"no changes made address book");         }         if (abaddressbooksave(addressbook, &error))         {             nslog(@"saved");         }         else {             nslog(@"not saved");         }            }      cfrelease(cfdataref);      // todo: release peoplepicker (and refactor code not have global)     [self dismissmodalviewcontrolleranimated:yes];      return no;  } 

but when debug , @ abaddressbookhasunsavedchanges returning false , image not getting set contact. might reason?

i wrote method in shouldcontinueafterselectingperson; have checked image , it's not null , abpersonsetimagedata returning true.

i have updated code , checked image set , if yes removing image , saving saving address book , relased cfdataref.

but still not working

update:2   have edited code , please @ changes.  

i have written code in method
-(bool)peoplepickernavigationcontroller:(abpeoplepickernavigationcontroller *)peoplepicker shouldcontinueafterselectingperson:(abrecordref)person { } ,
definately there person in address book . tried according above code still abaddressbookhasunsavedchanges returning false

you creating data wrong - should doing this.

uiimage *img = [uiimage imagewithcontentsoffile:path1]; nsdata *dataref = uiimagepngrepresentation(img); cfdataref cfdataref = cfdatacreate(null, [dataref bytes], [dataref length]); 

also try setting break point somewhere in beginning , check if isn't nil or doesn't have wrong value.

update

i have tried in actual xcode, test. reason why code tells address book has no unsaved changes because haven't added record inside yet - otherwise setting image on contact hasn't been added address book yet (that not problem, can without problems, contact not in address book until save it). magical line of code this:

abaddressbookaddrecord(addressbook, person, &error); 

this full code i've been using.

   cferrorref error = nil;     abaddressbookref addressbook = abaddressbookcreate();     abrecordref person = abpersoncreate();     nsstring *path1 = [[nsbundle mainbundle] pathforresource:@"sky_main" oftype:@"jpg"];      uiimage *img = [uiimage imagewithcontentsoffile:path1];     nsdata *dataref = uiimagepngrepresentation(img);     cfdataref cfdataref = cfdatacreate(null, [dataref bytes], [dataref length]);      if (abpersonhasimagedata(person)) {         abpersonremoveimagedata(person, &error);         abaddressbooksave(addressbook, &error);     }     abaddressbookaddrecord(addressbook, person, &error);      if (abpersonsetimagedata(person, cfdataref, &error)) {         if (abaddressbookhasunsavedchanges(addressbook)) {             nslog(@"has unsaved changes");         } else {             nslog(@"nothing save");         }         if (abaddressbooksave(addressbook, &error)) {             nslog(@"saved");         } else {             nslog(@"not saved");         }     }       abunknownpersonviewcontroller *ctr = [[abunknownpersonviewcontroller alloc] init];     ctr.unknownpersonviewdelegate = nil;     ctr.displayedperson = person;     ctr.allowsaddingtoaddressbook = yes;     ctr.allowsactions = yes;     ctr.hidesbottombarwhenpushed = yes;      // [[[ccdirector shareddirector] view] addsubview:ctr.view];     cfrelease(person);     cfrelease(addressbook); 

i using addressbookui, don't have use (i wanted see changes unsaved contact).

update 2

ok, created new project try. looks problem abaddressbookref. have peoplepicker argument of delegate method. since delegate method don't have add record because exists. code has work (tried 2 minutes ago). let me know :)

- (bool)peoplepickernavigationcontroller:(abpeoplepickernavigationcontroller *)peoplepicker shouldcontinueafterselectingperson:(abrecordref)person {      cferrorref error = nil;     abaddressbookref addressbook = peoplepicker.addressbook;     nsstring *path1 = [[nsbundle mainbundle] pathforresource:@"cat" oftype:@"jpg"];      uiimage *img = [uiimage imagewithcontentsoffile:path1];     nsdata *dataref = uiimagepngrepresentation(img);     cfdataref cfdataref = cfdatacreate(null, [dataref bytes], [dataref length]);      if (abpersonhasimagedata(person)) {         abpersonremoveimagedata(person, &error);         abaddressbooksave(addressbook, &error);     }      if (abpersonsetimagedata(person, cfdataref, &error)) {         if (abaddressbookhasunsavedchanges(addressbook)) {             nslog(@"has unsaved changes");         } else {             nslog(@"nothing save");         }         if (abaddressbooksave(addressbook, &error)) {             nslog(@"saved");         } else {             nslog(@"not saved");         }     }      cfrelease(addressbook);      return yes; } 

btw., since delegate method do not release person otherwise crash horribly - kidding, crash, because tries access memory doesn't exist.


Comments