How can I get the RGB value of a particular pixel in a UIImage?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- SwiftUI: UIImage (QRCode) does not load after call
- how do you prevent page scroll in textarea on mobi
相关文章
- Could I create “Call” button in HTML 5 IPhone appl
- Unable to process app at this time due to a genera
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
- Custom Marker performance iOS, crash with result “
- Open iOS 11 Files app via URL Scheme or some other
- Can keyboard of type UIKeyboardTypeNamePhonePad be
- Can not export audiofiles via “open in:” from Voic
You can't access the raw data directly, but by getting the CGImage of this image you can access it. here is a link to another question that answers your question and others you might have regarding detailed image manipulation : CGImage
First of all create and attach tap gesture recognizer allow allow user interactions:
Now implement
-tapGesture:
This will works on UILabel with transparent background, if this is not what you want you can compare alpha, red, green, blue with
self.label.backgroundColor
...Here's a generic method for getting pixel color in a UI image, building on Minas Petterson's answer:
Note that X and Y may be swapped; this function accesses the underlying bitmap directly and doesn't consider rotations that may be part of the UIImage.
Some Swift code based on Minas' answer. I've extended UIImage to make it accessible everywhere, and I've added some simple logic to guess the image format based on the pixel stride (1, 3, or 4)
Swift 3:
Updated for Swift 4:
Try this very simple code:
I used to detect a wall in my maze game (the only info that I need is the alpha channel, but I included the code to get the other colors for you):