我想在parse时保存UIImage而不是nil值



我正在实现保存帖子部分,帖子包含用户个人资料图片时,它被保存显示在主页上。但是会有一个用户没有头像。我试过这段代码,出现错误。

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文件在assets.

我的问题是.....

如何上传我的avatarplaceHolder ?

或者有更好的方法来覆盖nil值?

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

我猜你必须包装你的UIImage到一个PFFile如下:

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

最新更新