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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:12:03+00:00 2026-06-01T14:12:03+00:00

In my view I have few subviews. This is UIImageView. Each UIImageView contain image

  • 0

In my view I have few subviews. This is UIImageView.

Each UIImageView contain image with alpha chanel.

This is an image:
enter image description here

I use this method below for detecting my touch in location view:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    NSArray *views = [self.view subviews];

    for (UIView *v in views) {
        if([v isKindOfClass:[Piece class]]){
            if (CGRectContainsPoint(v.frame, touchLocation) && ((Piece *)v).snapped == FALSE) { 

                UITouch* touchInPiece = [touches anyObject]; 
                CGPoint point = [touchInPiece locationInView:(Piece *)v];
                BOOL solidColor = [self verifyAlphaPixelImage:(Piece *)v atX:point.x atY:point.y];

                if (solidColor) {                    
                    dragging = YES;
                    oldX = touchLocation.x;
                    oldY = touchLocation.y;
                    piece = (Piece *)v;
                    [self.view bringSubviewToFront:piece];
                    break;
                }              

            }
        }
    }
}

and this method for verify alpha pixel

- (BOOL)verifyAlphaPixelImage:(Piece *)image atX:(int)x atY:(int)y{

    CGImageRef imageRef = [image.image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = malloc(height * width * 4);
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                 bitsPerComponent, bytesPerRow, colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);    
    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
    //    CGFloat red   = (rawData[byteIndex]     ) ;
    //    CGFloat green = (rawData[byteIndex + 1] ) ;
    //    CGFloat blue  = (rawData[byteIndex + 2] ) ;
    CGFloat alpha = (rawData[byteIndex + 3] ) ;   
    NSLog(@"%f", alpha);
    free(rawData);    
    if(alpha==255.0) return NO;
    else return YES;



}

If alpha pixel founded I need to touch other UIImageView below UIImageView that I have tapp’s before.

For example if I have stacked UIImageView and I touch on first:

now I should verify first UIImageView

if I touched on alpha pixel – > I should move to next UIImageView with this coord and verify it for alpha pixel too.

If second 3th, 4th or 5th have not alpha pixel with my coord I should select this UIImageView.

For now I verify my pixel – but my method return wrong value.

  • 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-01T14:12:04+00:00Added an answer on June 1, 2026 at 2:12 pm
    1. At the discussion for How to get pixel data from a UIImage (Cocoa
      Touch) or CGImage (Core Graphics)?
      , there’s a correction to the
      routine you’re using. Use calloc instead of malloc, or you’ll start
      getting arbitrary results.
    2. If your Piece class ever scales images, you will need to scale its x and y inputs by the scale the image is being displayed at.
    3. The touchesBegan method oddly looks at some other view it contains, not itself. Is there a reason for this? What class is touchesBegan in?
    4. Subviews are stored in drawing order from back to front, so when you iterate through subviews, you’ll look at (and potentially select) the rearmost object first. Iterate from the last element of subviews towards the front instead.
    5. Even after this works, it will be very inefficient, rendering every Piece image every time the user taps. You’ll ultimately want to cache the pixel data for every Piece for rapid lookup.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this delegate method below that is setting a few fields in my
I have an admin view which contains four foreign keys each with a few
I have a View with few subviews in it and I draw some sublayers
I have few doubts regarding the 3d Animation Image View on android.i have created
I need to release uiimage/view/subviews when I want, and have a few questions regarding
I have disabled interactions in my primary view (which contains some subviews I use
I have a few methods inside my view controller that load up for future
I have a view with a few checkboxes that can be selected or unselected.
In a word, how? I have a view controller that creates a few custom
I have to print/display the values of few variables in a MVC view. How

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.