I have converted Swift 2.2 code to Swift 3.0, but I'm getting the following error.
open func saveToPath(_ path: String, format: ImageFormat, compressionQuality: Double) -> Bool
{
if let image = getChartImage(transparent: format != .jpeg) {
var imageData: Data!
switch (format)
{
case .png:
imageData = NSUIImagePNGRepresentation(image)
break
case .jpeg:
imageData = NSUIImageJPEGRepresentation(image, CGFloat(compressionQuality))
break
}
let url = NSURL(string: path)
return imageData.write(to: url as! URL, options: true)
}
return false
}
error :
Cannot convert value of type 'Bool' to expected argument type 'data.writeOptions' (aka 'NSData.writingOptions'))
What's wrong with this code?
The following two lines need to be fixed:
let url = NSURL(string: path)
return imageData.write(to: url as! URL, options: true)
URL
, not NSURL
.options
parameter.The fixed code should be like:
let url = URL(fileURLWithPath: path)
return imageData.write(to: url, options: .atomic)