How to operate a image file

Excuse me, it may be a fool question. But how to load an image, access to its pixels and save it to disk. I used to programming in C++ via OpenCV. And I'm a new player in swift. Thanks!

Answered by ssmith_c in 754106022

the search terms you are looking for are NSImage (macOS) or UIImage (iOS/iPadOS). Images have methods for loading from and writing to a URL. Images can be rendered into a CGContext. CGContexts provide access to their pixel buffers (and various higher level operations which manipulate those buffers).

Accepted Answer

the search terms you are looking for are NSImage (macOS) or UIImage (iOS/iPadOS). Images have methods for loading from and writing to a URL. Images can be rendered into a CGContext. CGContexts provide access to their pixel buffers (and various higher level operations which manipulate those buffers).

a URL can point at a local file (the scheme is 'file', so such URLs begin "file://"). For example, if you use NSOpenPanel on macOS to enable the user to select the file(s) to open, you'll get an array of URLs from the panel. See https://developer.apple.com/documentation/appkit/nsopenpanel. NSImage supports jpeg format input.