Swift 我想在解析时保存UIImage而不是nil值

Swift 我想在解析时保存UIImage而不是nil值,swift,parse-platform,uiimage,Swift,Parse Platform,Uiimage,我正在实现保存帖子部分,当保存帖子以便在主页上显示时,帖子包含用户配置文件图片。但会有一个用户没有个人资料图片。我试过这个代码,它出现了错误 if let profilePicture = PFUser.currentUser()?.objectForKey("profile_picture") { post["profile_picture"] = profilePicture } else { post["profile_picture"] = UIImage(named:

我正在实现保存帖子部分,当保存帖子以便在主页上显示时,帖子包含用户配置文件图片。但会有一个用户没有个人资料图片。我试过这个代码,它出现了错误

if let profilePicture = PFUser.currentUser()?.objectForKey("profile_picture") {
    post["profile_picture"] = profilePicture
} else {
    post["profile_picture"] = UIImage(named: "AvatarPlaceholder" )
}
post.saveInBackgroundWithBlock({ ( isSucessful: Bool, error : NSError?) -> Void in
    if error == nil {
        self.alert("success", message : "your post has been uploaded")
    } else {
        self.alert("Error", message : (error?.localizedDescription)!)
    }
如果用户有个人资料照片,就可以了。但如果不是,那就是问题所在

我在资产中有Avatarplaceholder jpeg文件

我的问题是……

我如何上传我的avatarplaceHolder

还是有更好的方法覆盖零价值


实际上,我不想浪费我的云存储。

我想您必须将UIImage包装到PFFile中,如下所示:

let imageData = UIImageJPEGRepresentation(UIImage(named: "AvatarPlaceholder")!, 0.5)!
let profileImageFile = PFFile(name: "profileImage", data: imageData)
post["profile_picture"] = profileImageFile

它是PNG,我修改了你的代码,让imageData=UIImagePngPresentation(UIImage(名为:“AvatarPlaceholder”)!)让profileImageFile=PFFile(名为:“profileImage.PNG”,数据:imageData!)post[“profile_picture”]=profileImageFile它不工作。你明白原因了吗?2015-10-12 19:51:59.834班尼[4650:1872022]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“不能对PFObject上的键或值使用nil。”。对值使用NSNull。调试它并检查哪个值为nil。也许你们的资产并没有命名为“AvatarPlaceholder”。那个么我必须知道如何调试它,而AvatarPlaceholder就是它的正确名称。谢谢你,伙计!