Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 带有实际数据的UIImage init返回nil_Swift_Uitableview - Fatal编程技术网

Swift 带有实际数据的UIImage init返回nil

Swift 带有实际数据的UIImage init返回nil,swift,uitableview,Swift,Uitableview,你好!这让我很生气,因为我不明白发生了什么事。 我有两个具有类似逻辑的TableViewController。 我使用1个数据模型。此模型包含用户信息,每个实体始终有一张照片要初始化它,我使用以下代码: var partnerPhoto: UIImage? = nil if let imageData = partners[indexPath.item].userPhoto { partnerPhoto = UIImage(data: imageData as! Data) } 在调试p

你好!这让我很生气,因为我不明白发生了什么事。
我有两个具有类似逻辑的TableViewController。
我使用1个数据模型。
此模型包含用户信息,每个实体始终有一张照片

要初始化它,我使用以下代码:

var partnerPhoto: UIImage? = nil
if let imageData = partners[indexPath.item].userPhoto {
   partnerPhoto = UIImage(data: imageData as! Data)
}
在调试
partners[indexPath.item]中,userPhoto
具有真实数据,甚至
imageData
显示4213字节。
但是,应用程序崩溃,出现典型错误:

致命错误:在展开可选值时意外发现nil

编辑:

解决方案: 事实上,我还没有找到这个问题的具体解决方案,我只是将一些逻辑从模型转移到视图控制器。在模型中,我确实从URL加载到数据。现在我在ViewController中做的很好,它工作得很好,但对我来说很奇怪,因为我刚刚做了cmd-xcmd-v

试试这个:

var partnerPhoto: UIImage?
guard let imageData = partners[indexPath.item].userPhoto else {return}
guard let image = UIImage(data: imageData) else {return}
partnerPhoto = image

在作用域之前强制转换imageData:

if let imageData = partners[indexPath.row].userPhoto as? Data {
    partnerPhoto = UIImage(data: imageData)
} else {
    partnerPhoto = UIImage() //just to prevent the crash
    debugPrint("imageData is incorrect")
    //You can set here some kind of placeholder image instead of broken imageData here like UIImage(named: "placeholder.png")
}
cell.fillWithContent(partnerPhoto: partnerPhoto)
return cell
此外,如果您能提供更多详细信息-如何以及何时启动
profilePhoto
,这将非常有用
cell.fillWithContent(partnerPhoto:partnerPhoto!、selfPhoto:profilePhoto!)
code


另外,您可以在
单元格上设置断点。在调用函数之前,使用内容填充
并检查partnerPhoto和/或profilePhoto是否为零。

在我的示例中,返回零,因为我尝试通过AlamofireFirebaseStorage下载图像,然后保存到核心数据。将核心数据正确保存,我的photoData不是nil,而是UIImage(数据:photoData as Data)
返回nil。 我通过检索图像本机方法FirebaseStorage解决了此问题:

func retrieveUserPhotoFromStorage(imageUID: String?, completion: @escaping (Result<Data, DomainError>) -> Void) {
    guard let imageID = imageUID else { return }
    let storageRef = storage.reference(withPath: DatabaseHierarhyKeys.users.rawValue).child(imageID)
    storageRef.getData(maxSize: Constant.maxSize) { data, error in
        guard let error = error else {
            if let data = data {
                completion(.success(data))
                return
            }
            completion(.failure(.domainError(value: "errorRetrieveImage data nil")))
            return
        }
        completion(.failure(.domainError(value: "errorRetrieveImage \(error.localizedDescription)")))
    }
}
func retrieveUserPhotoFromStorage(imageUID:String?,完成:@escaping(Result)->Void){
guard let imageID=imageUID else{return}
让storageRef=storage.reference(路径:DatabaseHierarhyKeys.users.rawValue).child(imageID)
storageRef.getData(maxSize:Constant.maxSize){data,错误在
guard let error=error else{
如果let data=data{
完成(.success(数据))
返回
}
完成(.failure(.domainError(值:“errorRetrieveImage数据为零”))
返回
}
完成(.failure(.domainError(值:“errorRetrieveImage\(error.localizedDescription)”))
}
}

然后将其保存到核心数据和UIImage(数据:photoData as Data)
已经不是零。

哪一行正是导致错误的原因?为什么强制转换
imageData
?在
if let
行执行强制转换。在'cellForRowAt'方法中,它应该返回uitableviewcell。在主帖子中,我将添加更多的代码,在其中我将合成我的cell-cell.fillWithContent。在我看来,它只是不能从数据中初始化映像。我一直认为问题在于映像。您的数据可能已损坏。尝试在fillWithContent调用此条件之前添加guard let image=UIImage(数据:imageData)else{return cell}
func retrieveUserPhotoFromStorage(imageUID: String?, completion: @escaping (Result<Data, DomainError>) -> Void) {
    guard let imageID = imageUID else { return }
    let storageRef = storage.reference(withPath: DatabaseHierarhyKeys.users.rawValue).child(imageID)
    storageRef.getData(maxSize: Constant.maxSize) { data, error in
        guard let error = error else {
            if let data = data {
                completion(.success(data))
                return
            }
            completion(.failure(.domainError(value: "errorRetrieveImage data nil")))
            return
        }
        completion(.failure(.domainError(value: "errorRetrieveImage \(error.localizedDescription)")))
    }
}