当前位置: 首页 > 面试题库 >

使用ABPersonCopyImageData获取iOS联系人图像

温星华
2023-03-14
问题内容

我正在尝试阅读我的iOS通讯录的联系人图像,但遇到了一些
麻烦。

我有一个读取ABAddressBookRef的联系人信息
并将其放在结构上的方法。

func readContacts(addressBook: ABAddressBookRef){
        let allPeople = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as NSArray
        var numeros : [Numeros] = []

        //lendo todos os contatos
        for person: ABRecordRef in allPeople{

            let firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty).takeRetainedValue() as! String
            let lastName = ABRecordCopyValue(person, kABPersonLastNameProperty).takeRetainedValue() as! String

            let phones : ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef

            let data = ABPersonCopyImageData(person).takeRetainedValue() as NSData
            let imagemContato = UIImage(data: data)

            for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++) {
                let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)

                let phoneNumber : NSString = phoneUnmaganed.takeUnretainedValue() as! NSString

                let locLabel : CFStringRef = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ? ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as CFStringRef : ""

                let cfStr:CFTypeRef = locLabel
                let nsTypeString = cfStr as! NSString
                let swiftString:String = nsTypeString as String

                //let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))

                let numero: Numeros = Numeros(tipoNumero: swiftString, numero: phoneNumber as String)

                numeros.append(numero)

            }

            let registroContato: Contato = Contato(primeiroNome: firstName, ultimoNome: lastName, contatoRecord: person, numeros: numeros, imagemContato: imagemContato!)

            contatos.append(registroContato)

        }
    }

but when I debug it, it stops on:

let data = ABPersonCopyImageData(person).takeRetainedValue() as NSData

并且不显示任何错误消息。

我究竟做错了什么?

我正在使用模拟器的默认联系人对其进行测试,他们没有图像,
但是有占位符及其名字的第一个字母。

编辑:

我将代码更改为

let imagemContato: UIImage?
            if let data = ABPersonCopyImageData(person)?.takeRetainedValue() as? NSData     {
                print("\(firstName) with image")
                imagemContato = UIImage(data: data)
            }
            else {
                print("\(firstName) without image")
                imagemContato = UIImage(named: "UooQo.jpg")
            }

并且在控制台上,所有联系人都没有图像:

控制台输出:凯特(Kate)没有图像丹尼尔(Daniel)没有图像约翰(John)没有图像
安娜(Anna)没有图像汉克(Hank)没有图像大卫(David)没有图像


问题答案:

我之前在项目中遇到的同样问题。试试这个,

//获取图片

   let img = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail).takeRetainedValue()

//在ImageView中设置图像

YourImageView.image=UIImage(data: img as! NSData)


 类似资料:
  • 问题内容: 我知道ios swift有一个Contacts Framework ,可以在其中获取联系人,但是找不到任何方法可以一起获取所有联系人,而我可以从该数组访问每个联系人。所有用于获取联系人的方法似乎都需要某种条件。有什么方法可以将所有联系人聚集在一起? 谢谢 问题答案: 有关Contact Framework问题的许多答案都建议对各种容器(帐户)进行迭代。但是,Apple文档将“统一联系人

  • 问题内容: 我想获取联系人姓名,但无法。看完这个答案后,我尝试使用family,给定的和显示的来获取名称,但没有任何效果 / 问题答案: 尝试以下代码以获取特定号码的联系方式 更多详细信息请参见下面的链接https://tausiq.wordpress.com/2012/08/23/android-get-contact- details-id-name-phone-photo/

  • 我用下面的代码连接到Graph API 但是我得到错误消息 *invoke-restmethod:{“error”:{“code”:“authorization_requestdenied”,“message”:“权限不足,无法完成操作。”,“innererror”:{“date”:“2020-09-04t17:54:13”,“request-id”:“2113f712-f022-4ebc-826

  • 问题内容: 是否可以在一个查询中完成? 据我所知sim联系人的内容uri是 问题答案: 这很容易!:)

  • 我正在尝试使用Microsoft图形API检索Office 365帐户的联系人列表。当我使用hotmail帐户登录时,一切正常。但是,如果我使用Office 365帐户,我在调用“https://graph.microsoft.com/beta/me/contacts”后会收到以下响应错误 我已尝试在Google上搜索错误,但找不到任何信息来告诉我为什么Office 365帐户不起作用。任何帮助将

  • 本文向大家介绍Android获取手机联系人信息,包括了Android获取手机联系人信息的使用技巧和注意事项,需要的朋友参考一下 Android如何获取手机联系人信息,本文为大家揭晓。 获取手机联系人信息步骤: 1、获取 ContentResolver ContentResolver resolver = getContentResolver(); 2、resolver.query(*)查询信息 查