我正在实现保存帖子部分,帖子在保存以在主页上显示时包含用户个人资料图片。但会有一个用户没有个人资料图片。我尝试了这段代码,出现错误。
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
关于swift - 我想在解析时保存 UIImage 而不是 nil 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33091420/