Skip to content

pixelColorAt extension on UIImage doesn't always return the correct colour #38

@simonmitchell

Description

@simonmitchell

Expected Behaviour

UIImage().pixelColorAt(_ point: CGPoint) -> UIColor? should always return the correct colour for a given pixel in an image.

Current Behaviour

In BRC First Aid, when using a point at CGPoint(x: image.width/2, y: 3) to retrieve the colour of a badge icon, the method returns a white colour from y values all the way from 0 to 30 odd, and then after that returns incorrect values. The image in question only has 1 translucent pixel at the top middle location.

Possible Solution

It's possible this is a scaling issue, or a colourspace issue. According to some S.O. posts, the pixel colours are picked from the original asset, so if an @2x asset then we need to double our coordinate passed in to get the correct value.

I investigated this by instead using CGPoint(x: image.width, y: 3) (As I was using an @2x image) but it seemed to return white and the correct colour for alternating y values so that also must not be entirely correct. Another S.O. post showed a generic solution which draws any image to a RGBA color space rather than assuming the original asset is the correct RGBA which is what we should attempt to use.

Steps to Reproduce (for bugs)

  1. Download latest BRC Emergency app.
  2. Complete quiz.
  3. Note that finish button is white (Or translucent) and un-clickable.

Your Environment

  • ThunderBasics version: 1.2.2
  • iOS / macOS version: iOS 12.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions