How can I automatically find the coordinates of the borders inside a PNG with transparency?

Imagine: I have a PNG image showing a teddy bear or triangle. Something non-square. Now I want to know at what point for a given line and direction (coordinates relative to the UIImageView coordinate system) the actual visible image starts.

Example: Let's say I need to know where the teddy bear's legs start from the left by looking at the last line. I am sure not only frame.origin.x, because the legs are not square. It can start somewhere at x = 12.

I would somehow iterate over the image data and check the pixels, "Hey, are you transparent?" And if so, I move on to the next one. "Hey, what's wrong with you? Transparent?" Until I get the result: "No, I am absolutely opaque!". Then I know: "That's right! Here the legs start in PNG! This is the border!".

Then I do this for each line and get some path coordinates. I personally need this to find the perfect spinning point as I want it to play realistically on the floor. I can't just use the frame width and origin information for this. It would be unrealistic.

So: is there a way to introspect UIImage data, or an image in general, to check if a pixel is transparent or not?

+1


a source to share


3 answers


You may find it easier to create an in-memory context with a known format and then render the image in the in-memory context. Note that the destination context may be a different bitmap format and color mode than the original for easy reading.

Alternatively, if you are a punishment glutton, you can get the CGImage UIImage property and then use CoreGraphics' functions to determine the pixel format, then write code to decode the pixel format data ...



The last option if you are controlling the images used: Create a mask image (1-bit alpha) from the image, and then use that to determine where the edges are. You can easily create such a mask in a graphics editor, or you could do it by drawing the image into a 1-bit context in memory if you set the drawing properties correctly.

+1


a source


I found this tutorial on the net where you can get the UIColor (ARGB) value of each pixel on the screen. maybe if you tweek that a bit you can pass the UIImage to it and get all the values ​​you need



+3


a source


It looks like this is not a very easy way to do it with UIImage. Instead, I see two simpler options.

Ideally, if you are managing the images you use, you can pre-compute the data you want. Then you can reformat the images so that the point of interest is in the center of the image (which you can easily compute on the client side with only width and height) or send the coordinate with the image. This allows the client to recalculate for each image, which can potentially speed up your application and reduce battery life.

Alternatively, image libraries such as libpng can be copied statically into your program. You can upload the image, process it, then upload it and transfer the file to UIImage. Only the functions you use will be related, so you can avoid adding too much bloat as render routines can be omitted. This has the disadvantage that your software relies on third party libraries.

+2


a source







All Articles