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!

Accepted Reply

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).

  • But how to load from or write to a .jpg file, not a URL.

Add a Comment

Replies

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).

  • But how to load from or write to a .jpg file, not a URL.

Add a Comment

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.