質問

I need to get the image url for each contact from AddressBook. I can get the image now, but the thing is I need to get the asset URL of the image for the particular person. Currently I'm getting the contact image by

[UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageData(person)]

Please help me to get the asset URL for that particular image. Thanks

役に立ちましたか?

解決

I think you need to make a local copy of the data, and then save a refereance to that local copy in your database:

//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
   }

他のヒント

A simple solution for SWIFT 3 and above.

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
        }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top