سؤال

أحاول تعيين صورة الاتصال مع الكود أدناه. لا أرى أي أخطاء ، ولا يتم حفظ الصورة في إدخال الاتصال في دفتر العناوين. الرجاء المساعدة! لا بد لي من أن تفعل شيئا خاطئا...

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
[abcontroller setDisplayedPerson:person];

UIImage *im = [UIImage imageNamed:@"image1.jpg"];
NSData *dataRef = UIImagePNGRepresentation(im);
CFErrorRef error;
ABAddressBookRef addressBook = ABAddressBookCreate(); 

NSLog(@"Error:",error);
    if (ABPersonSetImageData(person, (CFDataRef)dataRef, &error))
    {
        NSLog(@"Set contact photo %@", error);
        if (ABAddressBookHasUnsavedChanges(addressBook))
        {
            NSLog(@"Changes made to address book");
        }
        else {
            NSLog(@"No changes made to address book");
        }

        if (ABAddressBookSave(addressBook, &error))
        {
            NSLog(@"Saved");

        }
        else {
            NSLog(@"Not saved");
        }


    }
    else {
        NSLog(@"Error saving contact photo %@", error);
    }
ABAddressBookSave(addressBook, &error);

[self dismissModalViewControllerAnimated:YES];    

return NO;
}

ها هو سجل الإخراج الخاص بي:

2010-01-17 21:58:35.465 Error:
2010-01-17 21:58:35.504 Set contact photo <ABPeoplePickerNavigationController: 0x19e9b0>
2010-01-17 21:58:43.497 No changes made to address book
2010-01-17 21:58:44.724 Saved

لست متأكدًا من سبب تسجيل كائن الخطأ ككائن AbpeOplePickerNavigationScontroller؟

هل كانت مفيدة؟

المحلول

بخصوص لست متأكدًا من سبب تسجيل كائن الخطأ ككائن AbpeOplePickerNavigationScontroller؟ - لأنك لا تهيئة بشكل صحيح error هنا:

CFErrorRef error;

تعيين nil هنا ، أو سيكون لها قيمة عشوائية (أو أكثر دقة ، بعض الذاكرة السابقة) ، والتي تشير بالمناسبة إلى ABPeoplePickerNavigationController هدف.

فيما يتعلق بالمزايا: هل أنت متأكد من الإشارة إلى person لقد انتقلت إلى طريقتك صالحة في سياق دفتر العناوين؟ سأحاول استخدام وظيفة مثل ABAddressBookGetPersonWithRecordID أولاً ، بدلاً من المرور ABPersonRefS حول وتوقع أن تكون صالحة لمراجع دفتر العناوين المختلفة.

نصائح أخرى

كل هذه الإجابة لم تنجح. يجب عليك ببساطة تغيير سطر الكود الخاص بك من:

ABAddressBookRef addressBook = ABAddressBookCreate(); 

إلى:

ABAddressBookRef addressBook = [peoplePicker addressBook];

وبهذه الطريقة تحصل على الإشارة إلى "دفتر العناوين" ، "الشخص" ينتمي إليه.

لقد حاولت أيضًا استخدامها لتعيين الصورة (الصورة) للشخص المحدد الحالي ولكنه يعرض التدليك على حفظه بنجاح ولكن في جرعة تكنولوجيا المعلومات الحقيقية لا تعمل !!!

بعد حفر الكود والمستندات التي توفرها Apple SDK ، لدي بعض الحل مع الحل التالي.

يرجى التحقق من الرمز أدناه. إنه يعمل بشكل جيد حقًا بالنسبة لي وآمل لك أيضًا.

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    @try {
        CFErrorRef cfError = nil;

        if (ABPersonHasImageData(person)) {
            NSLog(@"%s has an image", _cmd);

            //theAlert = [[UIAlertView alloc] initWithTitle:@"Contact already has an image" message:@"Are you sure you want to overwrite it?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Overwrite"];
            //[theAlert show];
            //[theAlert release];
            //Note: We do not yet handle the answer to this question
        }

        // Just a sanity check

        if ((nil == [imageView image])
            || (nil == UIImagePNGRepresentation([imageView image]))) {
            NSLog(@"%s NIL STUFF", _cmd);
        }

        // Not sure I even need to create and save an address book
        // Cf. https://devforums.apple.com/message/27512#27512
        ABAddressBookRef libroDirec = ABAddressBookCreate();
        ABPersonSetImageData(person, (CFDataRef) (UIImageJPEGRepresentation([imageView image], 1.0f)), &cfError);
        ABAddressBookAddRecord(libroDirec, person, &cfError);
        if (ABAddressBookSave(libroDirec, nil)) {
            NSLog(@"%s saved successfuly", _cmd);
        } else {
            NSLog(@"%s something bad happen while saving", _cmd);
        }
        CFRelease(libroDirec);
    }
    @catch (NSException * e) {

    }
    @finally {

    }
    // TODO: release peoplePicker (and refactor code to not have it global)
    [self dismissModalViewControllerAnimated:YES];

    return NO;

}

لقد راجعت الكود الخاص بك ولا يعمل كما ينبغي. لذلك قمت بإجراء تغييرتين.

ABAddressBookRef libroDirec = [peoplePicker addressBook];//don't create
        ABPersonSetImageData(person, (CFDataRef)UIImagePNGRepresentation(display.image), &cfError);
        ABAddressBookAddRecord(libroDirec, person, &cfError);

        if (ABAddressBookSave(libroDirec, nil)) {
            NSLog(@"\n%s saved successfuly", _cmd);
        } else {
            NSLog(@"\n%s something bad happen while saving", _cmd);
        }
        //CFRelease(libroDirec);

الآن يعمل كما يفترض.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top