草庐IT

iphone - 将联系人图像添加到地址簿 iPhone

coder 2024-01-12 原文

我正在开发应用程序,当我从联系人选择器中选择任何联系人时,该应用程序会更改联系人的联系人图片。

这是我到目前为止实现的代码,

(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 to address book");
        }
        else {
            NSLog(@"No changes made to address book");
        }
        if (ABAddressBookSave(addressBook, &error))
        {
            NSLog(@"Saved");
        }
        else {
            NSLog(@"Not saved");
        }       
    }

    CFRelease(cfDataRef);

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

    return NO;

}

但是当我调试并查看 ABAddressBookHasUnsavedChanges 时,它返回 false 并且图像未设置为联系人。可能是什么原因?

我在shouldContinueAfterSelectingPerson中写了这个方法;我检查了图像,它不为空,ABPersonSetImageData 返回 TRUE

我已经更新了代码,已经检查了图像集,如果是,那么我将删除该图像并保存地址簿和 relased CFDataRef。

还是不行

Update:2 
 I have edited the code , please look at the changes. 

我已经用这个方法写了我所有的代码
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person { } ,
所以肯定会有已经在地址簿中的人。但是我根据上面的代码尝试仍然返回 false ABAddressBookHasUnsavedChanges

最佳答案

你创建的数据是错误的——你应该这样做。

UIImage *img = [UIImage imageWithContentsOfFile:path1];
NSData *dataRef = UIImagePNGRepresentation(img);
CFDataRef cfDataRef = CFDataCreate(NULL, [dataRef bytes], [dataRef length]);

也尝试在开始的某处设置一个断点,并检查是否有内容不为 nil 或没有错误的值。

更新

我已经在实际的 Xcode 中尝试过,只是为了测试。您的代码告诉您地址簿没有未保存更改的原因是因为您尚未在其中添加记录 - 否则您正在为尚未添加到地址簿的联系人设置图像(即不是一个问题,你可以毫无问题地这样做,但联系人本身不在地址簿中,直到你保存它)。所以对你来说神奇的代码行可能是这样的:

ABAddressBookAddRecord(addressBook, person, &error);

这是我一直在使用的完整代码。

   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 to 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);

我也在用AddressBookUI,但你不一定要用(我只是想看看未保存的联系人的变化)。

更新 2

好的,刚刚创建了一个新项目只是为了尝试。看起来问题实际上出在 ABAddressBookRef 上。您必须从委托(delegate)方法的 peoplePicker 参数中获取它。由于它是委托(delegate)方法,因此您不必添加记录,因为它已经存在。这是必须工作的代码(2 分钟前试过)。让我知道:)

- (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 to save");
        }
        if (ABAddressBookSave(addressBook, &error)) {
            NSLog(@"saved");
        } else {
            NSLog(@"not saved");
        }
    }

    CFRelease(addressBook);

    return YES;
}

顺便说一句,因为它是委托(delegate)方法不要释放这个人,否则它会可怕地崩溃 - 开个玩笑,它会崩溃,因为它试图访问不存在的内存。

关于iphone - 将联系人图像添加到地址簿 iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741599/

有关iphone - 将联系人图像添加到地址簿 iPhone的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby - 从 Ruby 中的主机名获取 IP 地址 - 2

    我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge

  4. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  5. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  6. ruby-on-rails - 在 Ruby (on Rails) 中使用 imgur API 获取图像 - 2

    我正在尝试使用Ruby2.0.0和Rails4.0.0提供的API从imgur中提取图像。我已尝试按照Ruby2.0.0文档中列出的各种方式构建http请求,但均无济于事。代码如下:require'net/http'require'net/https'defimgurheaders={"Authorization"=>"Client-ID"+my_client_id}path="/3/gallery/image/#{img_id}.json"uri=URI("https://api.imgur.com"+path)request,data=Net::HTTP::Get.new(path

  7. python ffmpeg 使用 pyav 转换 一组图像 到 视频 - 2

    2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p

  8. ruby - 如何将便捷类方法添加到 ruby​​ 中的 Singleton 类 - 2

    假设我有一个这样的单例类:classSettingsincludeSingletondeftimeout#lazy-loadtimeoutfromconfigfile,orwhateverendend现在,如果我想知道使用什么超时,我需要编写如下内容:Settings.instance.timeout但我宁愿将其缩短为Settings.timeout使这项工作有效的一个明显方法是将设置的实现修改为:classSettingsincludeSingletondefself.timeoutinstance.timeoutenddeftimeout#lazy-loadtimeoutfromc

  9. ruby - 是否有将图像文件转换为 ASCII 艺术的命令行程序或库? - 2

    有这样的事吗?我想在Ruby程序中使用它。 最佳答案 试试这个http://csl.sublevel3.org/jp2a/此外,Imagemagick可能还有一些东西 关于ruby-是否有将图像文件转换为ASCII艺术的命令行程序或库?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/6510445/

  10. ruby-on-rails - 使用 Dragonfly 从 URL 分配图像 - 2

    我正在使用Dragonfly在Rails3.1应用程序上处理图像。我正在努力通过url将图像分配给模型。我有一个很好的表格:{:multipart=>true}do|f|%>RemovePicture?Dragonfly的文档指出:Dragonfly提供了一个直接从url分配的访问器:@album.cover_image_url='http://some.url/file.jpg'但是当我在控制台中尝试时:=>#ruby-1.9.2-p290>picture.image_url="http://i.imgur.com/QQiMz.jpg"=>"http://i.imgur.com/QQ

随机推荐