我收到此错误“实例成员'jpegData'不能用于'UIImage'类型;您是要使用此类型的值吗?”
@IBAction func saveButtonWasPressed(_ sender: UIBarButtonItem) {
if NoteNameLabel.text == "" || NoteNameLabel.text == "NOTE NAME" || noteDescriptionLabel.text == "" || noteDescriptionLabel.text == "Note Description..." {
let alertController = UIAlertController(title: "Missing Information", message:"You left one or more fields empty. Please make sure that all fields are filled before attempting to save.", preferredStyle: UIAlertController.Style.alert)
let OKAction = UIAlertAction(title: "Dismiss", style: UIAlertAction.Style.default, handler: nil)
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion: nil)
}
else {
if (isExesting == false) {
let noteName = NoteNameLabel.text
let noteDescription = noteDescriptionLabel.text
if let moc = managedObjectContext {
let note = Note(context: moc)
if let data: Data = UIImage.jpegData(compressionQuality: 1.0) {
note.noteImage = data as NSData as Data
}
note.noteName = noteName
note.noteDescription = noteDescription
saveToCoreData() {
let isPresentingInAddFluidPatientMode = self.presentingViewController is UINavigationController
if isPresentingInAddFluidPatientMode {
self.dismiss(animated: true, completion: nil)
}
else {
self.navigationController!.popViewController(animated: true)
}
}
}
}
else if (isExesting == true) {
let note = self.note
let managedObject = note
managedObject!.setValue(NoteNameLabel.text, forKey: "noteName")
managedObject!.setValue(noteDescriptionLabel.text, forKey: "noteDescription")
if let data: Data = UIImage.jpegData(compressionQuality: 1.0) {
managedObject!.setValue(data, forKey: "noteImage")
}
do {
try context.save()
我尝试使用UIImage类的jpegData(compressionQuality :)和pngData()实例方法,如文档所述,我可以但仍然有错误