使用 ABPersonCopyImageData 获取联系人图像数据
Get Contact Image Data using ABPersonCopyImageData
我正在尝试从 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 类型。出了什么问题?
我在 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
}
我正在尝试从 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 类型。出了什么问题?
我在 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
}