I'm working on an iOS app in Swift where I need to save a PNG image with a tint color applied. However, I'm facing an issue where the tint color doesn't seem to apply to the saved image as expected.
Here's the relevant part of my Swift code:
func saveOutputImageAsPNG() {
guard let outputImage = signature else {
return
}
// Create a UIImage with the selected background color
let coloredImage = outputImage.withTintColor(UIColor(bgColor), renderingMode: .alwaysTemplate)
if let pngData = coloredImage.pngData() {
// Choose a file URL to save the PNG image.
// You can modify this path as per your requirements.
if let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy_MM_dd_HHmmss"
// Get the current date and time as a string in the specified format
let currentDateTime = dateFormatter.string(from: Date())
let fileURL = documentsDirectory.appendingPathComponent("\(currentDateTime)_signature.png")
do {
try pngData.write(to: fileURL)
print("Saved as PNG: \(fileURL)")
} catch {
print("Error saving PNG: \(error)")
}
}
}
}
Despite setting the tint color (bgColor), the saved PNG image doesn't reflect this color change. I've double-checked that bgColor is correctly set.
Can anyone help me understand why the tint color isn't being applied to the saved PNG image and suggest a solution to achieve the desired result?