Ios 如何在后台加载联系人图像?

Ios 如何在后台加载联系人图像?,ios,swift,dispatch-queue,Ios,Swift,Dispatch Queue,我正在使用通讯录开发一个应用程序。我正在显示API中的呼叫历史记录,当我显示历史记录数据时,我会检查联系人簿中是否有联系人。如果可用,则显示联系人姓名和图像。 我在后台做这个过程,但是有时候,tableview卡住了,没有给出任何回应。 我使用以下代码执行上述过程: for i in (0..<arrdata.count-1) { self.count = self.count + 1 let dict_data = arrdata.value(forKey: String

我正在使用通讯录开发一个应用程序。我正在显示API中的呼叫历史记录,当我显示历史记录数据时,我会检查联系人簿中是否有联系人。如果可用,则显示联系人姓名和图像。 我在后台做这个过程,但是有时候,tableview卡住了,没有给出任何回应。 我使用以下代码执行上述过程:

for i in (0..<arrdata.count-1)
{
    self.count = self.count + 1
    let dict_data = arrdata.value(forKey: String(format: "%d", arguments: [i])) as! NSDictionary

    let dict = NSMutableDictionary()
    dict.setValue(dict_data["callstart"], forKey: "callstart")
    dict.setValue(dict_data["notes"], forKey: "notes")
    dict.setValue(dict_data["debit"], forKey: "debit")
    dict.setValue(dict_data["pattern"], forKey: "pattern")
    dict.setValue(dict_data["calltype"], forKey: "calltype")
    dict.setValue(dict_data["billseconds"], forKey: "billseconds")
    dict.setValue(dict_data["disposition"], forKey: "disposition")
    dict.setValue(dict_data["callednum"], forKey: "callnumber")

    DispatchQueue.global(qos: .background).async(execute:
    { () -> Void in
        let searchContact = Constants.appDelegate.searchForContactUsingPhoneNumber(phoneNumber: dict_data["callednum"] as? String) as [ContactEntry]
        if searchContact.count == 1
        {
            for contact in searchContact
            {
                dict.setValue(contact.name, forKey: "callednum")
                dict.setValue(contact, forKey: "historycontact")
                if contact.image == nil
                {
                    dict.setValue(nil, forKey: "contactimage")
                }
                else
                {
                    let new_image = contact.image?.scaleUIImageToSize( image: contact.image!, size: CGSize(width:250,height:250))
                    let imgdata = new_image?.jpeg(.lowest)
                    dict.setValue(imgdata, forKey: "contactimage")


                }
            }
        }
        else
        {
            dict.setValue(nil, forKey: "historycontact")
            dict.setValue(dict_data["callednum"], forKey: "callednum")
            dict.setValue(nil, forKey: "contactimage")

        }
    })
    dict.setValue(nil, forKey: "historycontact")
    dict.setValue(dict_data["callednum"], forKey: "callednum")
    dict.setValue(nil, forKey: "contactimage")
    dict.setValue(dict_data["callerid"], forKey: "callerid")
    self.arrHistory.append(dict)
}

DispatchQueue.main.async
{
    self.tblview.reloadData()
}
用于i in(0..Void in
让searchContact=Constants.appDelegate.searchForContactUsingPhoneNumber(phoneNumber:dict_data[“callednum”]as?字符串)作为[ContactEntry]
如果searchContact.count==1
{
用于searchContact中的联系人
{
dict.setValue(contact.name,forKey:“callednum”)
dict.setValue(联系人,forKey:“历史联系人”)
如果contact.image==nil
{
dict.setValue(无,forKey:“contactimage”)
}
其他的
{
让new_image=contact.image?.scaleUI图像大小(图像:contact.image!,大小:CGSize(宽度:250,高度:250))
设imgdata=new_image?.jpeg(.lower)
dict.setValue(imgdata,forKey:“contactimage”)
}
}
}
其他的
{
dict.setValue(无,forKey:“历史接触”)
dict.setValue(dict_data[“callednum”],forKey:“callednum”)
dict.setValue(无,forKey:“contactimage”)
}
})
dict.setValue(无,forKey:“历史接触”)
dict.setValue(dict_data[“callednum”],forKey:“callednum”)
dict.setValue(无,forKey:“contactimage”)
dict.setValue(dict_data[“callerid”],forKey:“callerid”)
self.arrHistory.append(dict)
}
DispatchQueue.main.async
{
self.tblview.reloadData()
}
在后台队列中,我检查了联系人是否可用

我需要在后台进行联系人搜索,并在后台设置数据,在主线程上设置API调用

如果有任何人有上述问题的解决方案,那么请帮助我


谢谢。

我可以在这里看到一些潜在的问题。在for循环中获取后台线程(取决于数据)可能会占用大量线程!这可能是导致应用程序无响应的原因。我也不完全确定您的目标是什么,因为当后台线程完成时,您不会重新加载tableview

我可以告诉您,您希望在开始时将其分派到后台队列中,执行所有处理,然后在末尾将其分派回主线程以更新tableview。比如:

DispatchQueue.global(qos: .background).async(execute: { () -> Void in
    for i in (0..<arrdata.count-1)
    {
        self.count = self.count + 1
        let dict_data = arrdata.value(forKey: String(format: "%d", arguments: [i])) as! NSDictionary

        let dict = NSMutableDictionary()
        dict.setValue(dict_data["callstart"], forKey: "callstart")
        dict.setValue(dict_data["notes"], forKey: "notes")
        dict.setValue(dict_data["debit"], forKey: "debit")
        dict.setValue(dict_data["pattern"], forKey: "pattern")
        dict.setValue(dict_data["calltype"], forKey: "calltype")
        dict.setValue(dict_data["billseconds"], forKey: "billseconds")
        dict.setValue(dict_data["disposition"], forKey: "disposition")
        dict.setValue(dict_data["callednum"], forKey: "callnumber")


        let searchContact = Constants.appDelegate.searchForContactUsingPhoneNumber(phoneNumber: dict_data["callednum"] as? String) as [ContactEntry]
        if searchContact.count == 1
        {
            for contact in searchContact
            {
                dict.setValue(contact.name, forKey: "callednum")
                dict.setValue(contact, forKey: "historycontact")
                if contact.image == nil
                {
                    dict.setValue(nil, forKey: "contactimage")
                }
                else
                {
                    let new_image = contact.image?.scaleUIImageToSize( image: contact.image!, size: CGSize(width:250,height:250))
                    let imgdata = new_image?.jpeg(.lowest)
                    dict.setValue(imgdata, forKey: "contactimage")


                }
            }
        }
        else
        {
            dict.setValue(nil, forKey: "historycontact")
            dict.setValue(dict_data["callednum"], forKey: "callednum")
            dict.setValue(nil, forKey: "contactimage")

        }
        dict.setValue(nil, forKey: "historycontact")
        dict.setValue(dict_data["callednum"], forKey: "callednum")
        dict.setValue(nil, forKey: "contactimage")
        dict.setValue(dict_data["callerid"], forKey: "callerid")
        self.arrHistory.append(dict)
    }

    DispatchQueue.main.async {
        self.tblview.reloadData()
    }
})
DispatchQueue.global(qos:.background).async(在中执行:{()->Void

对于(0)中的i,感谢您给出答案。我将尝试您的解决方案。