我有UIDocument的子类,我希望检查代码。这里是。我做得对吗?我想我做到了,但是我遇到了问题。我认为只要将文件保存到具有png扩展名的url中,此代码就可以正常工作。
UIDocument子类:
class ImageDocument: UIDocument {
var image: UIImage? = nil
override func load(fromContents contents: Any, ofType typeName: String?) throws {
guard let data = contents as? Data else {
throw DataFileError.fileReadFailed
}
image = UIImage(data: data)
}
override func contents(forType typeName: String) throws -> Any {
guard image != nil else {
throw DataFileError.badData
}
return image!.pngData() as Any
}
}
保存UIDocument子类的代码:
saveToURL.appendPathExtension("png")
let document = ImageDocument(fileURL: saveToURL)
document.image = image // UIImage object
document.save(to: document.fileURL, for: .forCreating) {
(success: Bool) in
print("document.save success - ", success)
print("document.fileURL=", document.fileURL)
}