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 7034581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:12:12+00:00 2026-05-28T01:12:12+00:00

I’m using this function to detect if the touched point of a UIImageView corresponds

  • 0

I’m using this function to detect if the touched point of a UIImageView corresponds to a transparent pixel or not.

-(BOOL)isTransparency:(CGPoint)point
{
    CGImageRef cgim = self.image.CGImage;

    unsigned char pixel[1] = {0};

    point.x = (point.x / (self.frame.size.width / CGImageGetWidth(cgim)));
    point.y = (point.y / (self.frame.size.height / CGImageGetHeight(cgim)));


    CGContextRef context = CGBitmapContextCreate(pixel, 
                                                 1, 1, 8, 1, NULL,
                                                 kCGImageAlphaOnly);

    CGContextSetBlendMode(context, kCGBlendModeCopy);

    CGContextDrawImage(context, CGRectMake(-point.x, 
                                           -(self.image.size.height - point.y), 
                                           CGImageGetWidth(cgim), 
                                           CGImageGetHeight(cgim)), 
                       cgim);

    CGContextRelease(context);

    CGFloat alpha = pixel[0]/255.0;

    return alpha < 0.01;

}

Works fine if the UIImageView transform is not modified. But when user rotates the image the transform matrix is modified, this code get the image in the original position, not corresponding to the transformed image position, and I’m getting false results.

My question is how can I detect if the touched pixel of the image of the UIImageView is transparent when it is rotated?

Thanks.

EDIT: -Version 1.1-

Hi. Thanks for the answer. I’ve modified the code and now this version still working as previous version:

-(BOOL)isTransparency:(CGPoint)point
{
    CGImageRef cgim = self.image.CGImage;

    NSLog(@"Image Size W:%lu H:%lu",CGImageGetWidth(cgim),CGImageGetHeight(cgim));

    unsigned char pixel[1] = {0};

    point.x = (point.x / (self.frame.size.width / CGImageGetWidth(cgim)));
    point.y = (point.y / (self.frame.size.height / CGImageGetHeight(cgim)));


    CGContextRef context = CGBitmapContextCreate(pixel, 
                                                 1, 1, 8, 1, NULL,
                                                 kCGImageAlphaOnly);

    CGContextSetBlendMode(context, kCGBlendModeCopy);

    CGContextTranslateCTM(context, self.image.size.width * 0.5f, self.image.size.height * 0.5f);
    CGContextConcatCTM(context, self.transform);
    //CGContextScaleCTM(context, [self currentPercent]/100, [self currentPercent]/100);
    //CGContextRotateCTM(context, atan2f(self.transform.b, self.transform.a));
    CGContextTranslateCTM(context, -self.image.size.width * 0.5f, -self.image.size.height * 0.5f);

    CGContextDrawImage(context, CGRectMake(-point.x, 
                                           -(self.image.size.height - point.y), 
                                           self.image.size.width, 
                                           self.image.size.height), 
                       cgim);

    CGContextRelease(context);

    CGFloat alpha = pixel[0]/255.0;

    NSLog(@"Is Transparent: %d",alpha < 0.01);

    return alpha < 0.01;

}

I tried with several functions to rotate and scale the original image to current rotation and size without success. 🙁

Any help please?

EDIT: Version 1.2

I found a workaround to fix this problem. I decided to create a context with size equal to screen size and draw only the touched object, according with the current transform matrix.

After that I create a bitmap for this context where the image is properly drawn and pass the image to the function that works only with non transformed images.

The result is that works ok. I don’t know if is the optimum way to do that, but works.

Here the code:

-(BOOL)isTransparency:(CGPoint)point
{
    UIGraphicsBeginImageContext(self.superview.bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    CGContextSetBlendMode(context, kCGBlendModeCopy);

    CGContextTranslateCTM(context, [self center].x, [self center].y);
    CGContextConcatCTM(context, [self transform]);
    CGContextTranslateCTM(context,
                          -[self bounds].size.width * [[self layer] anchorPoint].x,
                          -[self bounds].size.height * [[self layer] anchorPoint].y);

    [[self image] drawInRect:[self bounds]];

    CGContextRestoreGState(context);

    CGImageRef image = CGBitmapContextCreateImage(context);

    CGImageRef viewImage = image;

    return [self isTransparentPixel:point image:image];

}

This function creates the image with the size of superview (in my case always full screen) and receives as parameter the touched point in UIKit coordinates system from the parent view.

-(BOOL)isTransparentPixel:(CGPoint)point image:(CGImageRef)cgim
{
    NSLog(@"Image Size W:%lu H:%lu",CGImageGetWidth(cgim),CGImageGetHeight(cgim));

    unsigned char pixel[1] = {0};

    CGContextRef context = CGBitmapContextCreate(pixel, 
                                                 1, 1, 8, 1, NULL,
                                                 kCGImageAlphaOnly);

    CGContextSetBlendMode(context, kCGBlendModeCopy);

    CGContextDrawImage(context, CGRectMake(-point.x, 
                                           -(self.superview.frame.size.height - point.y), 
                                           CGImageGetWidth(cgim), 
                                           CGImageGetHeight(cgim)), 
                       cgim);

    CGContextRelease(context);

    CGFloat alpha = pixel[0]/255.0;

    return alpha < 0.01;

}

This function detects the alpha channel of a given pixel in a given image.

Thanks for all.

  • 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-05-28T01:12:13+00:00Added an answer on May 28, 2026 at 1:12 am

    Apply the transform from your image view to your CGContext with a function such as CGContextConcatCTM() before drawing your image.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.