How do I mirror a jpeg file, swift, macOS?
Using something like CGImageSourceCreateWithURL would be good. I would like to change a property and then write the file out again. Not sure if this is possible.
Another way appears to be drawing the image and then write this out to a file. The image needs to be scaled with a -1 scale and transformed so that it still appears in the destination correctly. I've not been able to find a swift example of doing this. This is an example that is not in swift. I cannot reproduce it in swift. Also using an NSImage requires decoding and then encoding the JPEG file. A solution that does not do this would be better. Flip NSImage on both axes
Edit: A vImage may be a middle ground. This will need to be populated by reading the JPEG but the data could be flipped without "drawing" it at a scale of -1. I think an NSImage could be made using initWithContentsOfURL or initByReferencingURL and a CGImage made from the NSImage using CGImageForProposedRect. The vImage made from the CGImage?
To mirror a NSImage
using vImage mirroring routines , you can do something like:
extension NSImage {
func mirrored() -> NSImage? {
guard
let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil),
let colorSpace = cgImage.colorSpace else {
return nil
}
var format = vImage_CGImageFormat(bitsPerComponent: UInt32(cgImage.bitsPerComponent),
bitsPerPixel: UInt32(cgImage.bitsPerPixel),
colorSpace: Unmanaged.passRetained(colorSpace),
bitmapInfo: cgImage.bitmapInfo,
version: 0,
decode: nil,
renderingIntent: cgImage.renderingIntent)
var source = vImage_Buffer()
var result = vImageBuffer_InitWithCGImage(
&source,
&format,
nil,
cgImage,
vImage_Flags(kvImageNoFlags))
guard result == kvImageNoError else { return nil }
defer { free(source.data) }
var destination = vImage_Buffer()
result = vImageBuffer_Init(
&destination,
vImagePixelCount(cgImage.height),
vImagePixelCount(cgImage.width),
UInt32(cgImage.bitsPerPixel),
vImage_Flags(kvImageNoFlags))
guard result == kvImageNoError else { return nil }
result = vImageHorizontalReflect_ARGB8888(&source, &destination, vImage_Flags(kvImageNoFlags))
guard result == kvImageNoError else { return nil }
defer { free(destination.data) }
return vImageCreateCGImageFromBuffer(&destination, &format, nil, nil, vImage_Flags(kvImageNoFlags), nil).map {
NSImage(cgImage: $0.takeRetainedValue(), size: size)
}
}
}
Thus, taking a screen snapshot of the above in my editor, I then mirrored
the image, resulting in:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.