Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:27:13+00:00 2026-06-14T01:27:13+00:00

How would you detect touches only on non-transparent pixels of a UIImageView , efficiently?

  • 0

How would you detect touches only on non-transparent pixels of a UIImageView, efficiently?

Consider an image like the one below, displayed with UIImageView. The goal is be to make the gesture recognisers respond only when the touch happens in the non-transparent (black in this case) area of the image.

enter image description here

Ideas

  • Override hitTest:withEvent: or pointInside:withEvent:, although this approach might be terribly inefficient as these methods get called many times during a touch event.
  • Checking if a single pixel is transparent might create unexpected results, as fingers are bigger than one pixel. Checking a circular area of pixels around the hit point, or trying to find a transparent path towards an edge might work better.

Bonus

  • It’d be nice to differentiate between outer and inner transparent pixels of an image. In the example, the transparent pixels inside the zero should also be considered valid.
  • What happens if the image has a transform?
  • Can the image processing be hardware accelerated?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:27:14+00:00Added an answer on June 14, 2026 at 1:27 am

    Here’s my quick implementation: (based on Retrieving a pixel alpha value for a UIImage)

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
        //Using code from https://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
    
        unsigned char pixel[1] = {0};
        CGContextRef context = CGBitmapContextCreate(pixel,
                                                     1, 1, 8, 1, NULL,
                                                     kCGImageAlphaOnly);
        UIGraphicsPushContext(context);
        [image drawAtPoint:CGPointMake(-point.x, -point.y)];
        UIGraphicsPopContext();
        CGContextRelease(context);
        CGFloat alpha = pixel[0]/255.0f;
        BOOL transparent = alpha < 0.01f;
    
        return !transparent;
    }
    

    This assumes that the image is in the same coordinate space as the point. If scaling goes on, you may have to convert the point before checking the pixel data.

    Appears to work pretty quickly to me. I was measuring approx. 0.1-0.4 ms for this method call. It doesn’t do the interior space, and is probably not optimal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I would like to detect if a segment only 'touches' a polygon or
I would like to know how to detect and differentiate between touches in a
I'd like to detect touches (or just a tap would do) on the background
I would like to detect when user touches screen constantly for 5 seconds. No
I would like to detect when a page load request give to a UIWebView
I would like to detect the following sequences: a aA aAa aAaA ... where
I have a database with almost 100,000 comments and I would like to detect
I have a JPanel, which I would like to detect the following events (1)
Using winforms in vs2008. I have a DataGridView and I would like to detect
I have a store with items retrieved from server. I would like to detect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.