如何从联系人获取联系人图像URL?

时间:2013-12-26 11:49:04

标签: ios iphone objective-c uiimage abaddressbook

我需要从AddressBook获取每个联系人的图片网址。我现在可以获得图像,但问题是我需要为特定的人获取图像的资产URL。目前我正在通过

获取联系人图片
[UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(person)]

请帮我获取该特定图片的资产网址。感谢

2 个答案:

答案 0 :(得分:2)

我认为您需要制作数据的本地副本,然后在数据库中保存对该本地副本的引用:

//create a fileName perhaps based on the contact name or a GUID
    NSError *err;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);//find the cache dir. You might want to consider using the doc dir instead
    NSString * path = [paths  objectAtIndex:0];
    path = [path stringByAppendingPathComponent:fileName];
   [imgData writeToFile:path options:NSDataWritingAtomic error:&err];
   if(!err)
   {
   ///save path to the DB
   }

答案 1 :(得分:0)

SWIFT 3及更高版本的简单解决方案。

var users = [User]()   
     func getContact(){
            if #available(iOS 9.0, *) {
                let contactStore = CNContactStore()
                _ = [
                    CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
                    CNContactEmailAddressesKey]

                // Get all the containers
                var allContainers: [CNContainer] = []
                do {
                    allContainers = try contactStore.containers(matching: nil)
                } catch {
                    print("Error fetching containers")
                }


                do {
                    try contactStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor])) {
                        (contact, cursor) -> Void in
                        if (!contact.emailAddresses.isEmpty){
                            //self.email.append(String(contact.emailAddresses))
                            for emailAdd:CNLabeledValue in contact.emailAddresses {
                                let a = emailAdd.value
                                if a as String != ""{
                                    for (index, data) in self.users.enumerated(){
                                        if a as? String == data.email{
                                            print("Emial found in Contact",index,a as String)
                                            if contact.isKeyAvailable(CNContactImageDataKey) {
                                                if let img = contact.imageData{
                                                    self.users[index] = User( id: self.users[index].id, firstName: self.users[index].firstName, lastName: self.users[index].lastName, email: self.users[index].email, user_type: self.users[index].user_type,user_image: UIImage(data: img))
                                                    print("imag",img)
                                                }
                                            }
                                        }
                                    }
                                    self.email.append(a as String)
                                }
                            }
                        }
                    }
                }
                catch{
                    print("Handle the error please")
                }
            }else{
                // Fallback
            }
            filteredData = email
            dropDown.dataSource = filteredData
        }