UIImage 作为可选
UIImage as Optional
嗨,我希望用户即使不选择个人资料照片也能注册
现在我将此代码用于个人资料图片:
let profileImageData = UIImageJPEGRepresentation((userImage.image!), 1)
if (profileImageData != nil) {
let profileImageFile = PFFile (data: profileImageData!)
myUser.setObject(profileImageFile!, forKey: "profile_picture")
}
每次用户在注册时没有选择个人资料图片时,我都会收到此错误
fatal error: unexpectedly found nil while unwrapping an Optional value
我希望用户即使值为 nil 也能注册。
非常感谢
我在这里修改你的代码:
if let profileImageData = UIImageJPEGRepresentation((userImage.image!), 1){
let profileImageFile = PFFile (data: profileImageData!)
myUser.setObject(profileImageFile!, forKey: "profile_picture")
}else{
print("No image selected")
}
希望对您有所帮助!
问题是您试图强制解包 userImage.image 变量,即使它没有设置。仅当您确定变量具有值时才应强制解包。
下面的修改应该有效
if let profileImage = userImage.image{
let profileImageData = UIImageJPEGRepresentation((profileImage), 1)
let profileImageFile = PFFile (data: profileImageData!)
myUser.setObject(profileImageFile!, forKey: "profile_picture")
}else{
//! Handle part when no image is selected
}
嗨,我希望用户即使不选择个人资料照片也能注册
现在我将此代码用于个人资料图片:
let profileImageData = UIImageJPEGRepresentation((userImage.image!), 1)
if (profileImageData != nil) {
let profileImageFile = PFFile (data: profileImageData!)
myUser.setObject(profileImageFile!, forKey: "profile_picture")
}
每次用户在注册时没有选择个人资料图片时,我都会收到此错误
fatal error: unexpectedly found nil while unwrapping an Optional value
我希望用户即使值为 nil 也能注册。
非常感谢
我在这里修改你的代码:
if let profileImageData = UIImageJPEGRepresentation((userImage.image!), 1){
let profileImageFile = PFFile (data: profileImageData!)
myUser.setObject(profileImageFile!, forKey: "profile_picture")
}else{
print("No image selected")
}
希望对您有所帮助!
问题是您试图强制解包 userImage.image 变量,即使它没有设置。仅当您确定变量具有值时才应强制解包。
下面的修改应该有效
if let profileImage = userImage.image{
let profileImageData = UIImageJPEGRepresentation((profileImage), 1)
let profileImageFile = PFFile (data: profileImageData!)
myUser.setObject(profileImageFile!, forKey: "profile_picture")
}else{
//! Handle part when no image is selected
}