我正在尝试拾取图像并将其转换为字符串以保存到模型var picture = ""
中。我可以选择图片,但它不会显示任何有关我的错误的建议。
更新:我通过这些更改找出了答案,但仍然在模型中使用了字符串。
var imagePath = ""
var profilePic: UIImage?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? URL, let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
ALAssetsLibrary().asset(for: referenceUrl, resultBlock: { asset in
let fileName = asset?.defaultRepresentation().filename()
let userModel = User()
userModel.picture = fileName!
let filePath = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/\(String(describing: fileName))"
self.imagePath = filePath
let imageData : NSData = UIImagePNGRepresentation(pickedImage)! as NSData
imageData.write(toFile: filePath, atomically: true)
UserDefaults.standard.set(imageData, forKey: "profileImage")
UserDefaults.standard.synchronize()
self.profilePic = pickedImage
self.tableView.reloadData()
}, failureBlock: nil)
}
dismiss(animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case Setting.preferences.rawValue:
guard let cell = tableView.dequeueReusableCell(withIdentifier: PreferencesTableViewCell.identifier,
for: indexPath) as? PreferencesTableViewCell else { return UITableViewCell() }
cell.addressTextfield.delegate = self
cell.cityTextField.delegate = self
cell.stateTextField.delegate = self
cell.zipcodeTextField.delegate = self
return cell
case Setting.personal.rawValue:
guard let cell = tableView.dequeueReusableCell(withIdentifier: PersonalTableViewCell.identifier,
for: indexPath) as? PersonalTableViewCell else { return UITableViewCell() }
cell.firstNameTextField.delegate = self
cell.lastNameTextField.delegate = self
cell.phoneNumberTextField.delegate = self
cell.emailTextField.delegate = self
cell.buttonTouchedClosure = { [weak self] in
self?.pickImage()
}
return cell
case Setting.notifications.rawValue:
guard let cell = tableView.dequeueReusableCell(withIdentifier: NotificationsTableViewCell.identifier,
for: indexPath) as? NotificationsTableViewCell else { return UITableViewCell() }
return cell
case Setting.social.rawValue:
guard let cell = tableView.dequeueReusableCell(withIdentifier: SocialTableViewCell.identifier,
for: indexPath) as? SocialTableViewCell else { return UITableViewCell() }
cell.logoutButton.addTarget(self, action: #selector(UserProfileTableViewController.logout), for: .touchUpInside)
return cell
default: return UITableViewCell()
}
}