2 Replies
      Latest reply: Jan 27, 2017 12:30 AM by aleksanderfromwarszawa RSS
      aleksanderfromwarszawa Level 1 Level 1 (0 points)

        Hi all,

        Here is the script

         

        import AppKit

        import Foundation

        import Cocoa

        let location = "/users/alex/Desktop/1122.png"

        let image = NSImage(byReferencingFile: location)

        let pixEl = getPixel(_image: UnsafeMutablePointer<Int>!, atX: 10, y: 10)

         

        And it takes a error in last line. Tell me please, what's wrong?

        • Re: Error Editor placeholder in sourse file in getPixel call
          eskimo Apple Staff Apple Staff (6,075 points)

          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"