Here is reproduction code for Xcode Playground:
import UIKit
import AVFoundation
let f = UIGraphicsImageRendererFormat()
f.scale = 1.0
let size = CGSize(width: 50, height: 1)
let image = UIGraphicsImageRenderer(size: size, format: f).image { rendererContext in
UIColor.green.setFill()
rendererContext.fill(CGRect(origin: .zero, size: size))
}
let tempDirURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
var tempFileURL = tempDirURL.appendingPathComponent(UUID().uuidString)
tempFileURL = tempFileURL.appendingPathExtension("heic")
guard let imageDestination = CGImageDestinationCreateWithURL(tempFileURL as CFURL, AVFileType.heic as CFString, 1, nil)
else { fatalError("unable to create CGImageDestination") }
CGImageDestinationAddImage(imageDestination, image.cgImage!, nil)
let result = CGImageDestinationFinalize(imageDestination)
print(result)
If width or height is 1px, CGImageDestinationFinalize
returns false and file is not created.
If height and width is at least 2px, image is saved without errors.
The same issue exists if I save using CIImage methods.