热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOSAddressBook-获取联系人图像崩溃-iOSAddressBook-getcontactimagecrash

ThereissomeproblemwithAddressBookwhichIcantreproduce,codeworksonmyiPhoneandiPad,th

There is some problem with AddressBook which I can't reproduce, code works on my iPhone and iPad, this happens on client phone and result in app crash. As far as I can see from Crashlytics problem should be in following line:

AddressBook有一些我无法重现的问题,代码可以在我的iPhone和iPad上运行,这种情况发生在客户端手机上并导致应用程序崩溃。据我所知,Crashlytics问题应该在以下几行:

let data = ABPersonCopyImageDataWithFormat(contact, kABPersonImageFormatThumbnail).takeRetainedValue()

Here is complete code for reading address book:

以下是阅读地址簿的完整代码:

    var err : Unmanaged? = nil
    let addressBookRef : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
    if addressBookRef == nil {
        print(err)
        return
    }

    let cOntacts= ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as NSArray as [ABRecord]
    for contact in contacts {
        let firstName = ABRecordCopyValue(contact, kABPersonFirstNameProperty)?.takeRetainedValue() as? String
        let lastName = ABRecordCopyValue(contact, kABPersonLastNameProperty)?.takeRetainedValue() as? String

        var image: UIImage?
        if ABPersonHasImageData(contact) {

            let data = ABPersonCopyImageDataWithFormat(contact, kABPersonImageFormatThumbnail).takeRetainedValue()
            if let img = UIImage(data: data) {
                image = img
            }

        }


        …
}

Do you have any suggestions what could happen on clients phone so I can reproduce this error? Is it possible that some contact is corrupt? How should I handle this?

您对客户手机上可能发生的事情有什么建议吗?我可以重现这个错误吗?某些联系是否可能已损坏?我该怎么处理?

I've seen this post Get iOS contact image with ABPersonCopyImageData that ABPersonCopyImageData could return nil, I tried to handle that but app is still crashing.

我看过这篇帖子用ABPersonCopyImageData获取iOS联系人图片,ABPersonCopyImageData可以返回nil,我试图处理,但应用程序仍在崩溃。

2 个解决方案

#1


1  

check all for nil. if ABPersonCopyImageDataWithFormat returns nil, you call takeRetainedValue on nil. and then use it nil to create image too

检查所有没有。如果ABPersonCopyImageDataWithFormat返回nil,则在nil上调用takeRetainedValue。然后使用它来创建图像

guard let CFData = ABPersonCopyImageDataWithFormat(contact, kABPersonImageFormatThumbnail) else {
    print("no cfdata")
    return
}

if let data = CFData.takeRetainedValue {
     if let img = UIImage(data: data) {
         image = img
     }
}

#2


0  

Maybe you should test the data itself and not the UIImage and also initialize an UIImageView instead of a UIImage.

也许您应该测试数据本身而不是UIImage,并初始化UIImageView而不是UIImage。

if let data = ABPersonCopyImageDataWithFormat(contact, kABPersonImageFormatThumbnail).takeRetainedValue()
    image = UIImage(data: data)
}

推荐阅读
author-avatar
adfa3sd5f6a
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有