getPixel(_:atX:y:)
is a method on NSBitmapImageRep. For this to work you’ll have to extract that image rep out of the NSImage. You can get the image reps from an image via the
representations
property. So, your code might look something like this:
import Cocoa
let location = "/Users/quinn/Desktop/test.png"
let image = NSImage(byReferencingFile: location)!
let rep = image.representations.first! as! NSBitmapImageRep
var pixel = [Int](repeating: 0, count: 4)
rep.getPixel(&pixel, atX: 400, y: 350)
for channel in pixel {
print(String(channel, radix: 16))
}
This is taking a lot of shortcuts. For example, it assumes that the image has a single image rep, that that image rep is a bitmap image rep, and that the bitmap has four channels.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"