【问题标题】:Get Contact Image Data using ABPersonCopyImageData使用 ABPersonCopyImageData 获取联系人图像数据
【发布时间】:2015-09-24 07:51:12
【问题描述】:

我正在尝试从 Mac 上的通讯录中获取联系方式。我可以获取名字和姓氏等字段,但我正在为 ABPersonCopyImageData 的语法苦苦挣扎。

现在根据文档 ABPersonCopyImageData 采用 ABPerson 类型的单个参数。

这是我的代码:

import AddressBook
let thisPerson : ABPerson
let addressBook = ABAddressBook.sharedAddressBook()
rec = addressBook.recordForUniqueId("0005A360-327F-4E12-BBB9-24A842497E12:ABPerson")
let firstName = rec.valueForProperty(kABFirstNameProperty) as! String
let lastName = rec.valueForProperty(kABLastNameProperty) as! String
println("\(firstName) \(lastName)")

let contactImage = ABPersonCopyImageData(thisPerson)

最后一行停止编译器并出现错误:无法使用类型为 (ABPerson) 的参数列表调用“ABPersonCopyImageData”。据我所知,thisPerson 是 ABPerson 类型。出了什么问题?

【问题讨论】:

    标签: macos swift abaddressbook


    【解决方案1】:

    我发现了如何在 ElCapitan 中做到这一点:

    import Contacts
    
    func getContactImage(name:String) -> NSImage?
    {
        let store = CNContactStore()
        do
        {
            let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(name), keysToFetch:[CNContactImageDataKey])
            if contacts.count > 0
            {
                if let image = contacts[0].imageData
                {
                    return NSImage.init(data: image)
                }
            }
        }
        catch
        {
        }
    
        return nil
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      相关资源
      最近更新 更多