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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:12:34+00:00 2026-05-15T09:12:34+00:00

Based on the responses to a previous question , I’ve created a category on

  • 0

Based on the responses to a previous question, I’ve created a category on UIImageView for extracting pixel data. This works fine in the simulator, but not when deployed to the device. I should say not always — the odd thing is that it does fetch the correct pixel colour if point.x == point.y; otherwise, it gives me pixel data for a pixel on the other side of that line, as if mirrored. (So a tap on a pixel in the lower-right corner of the image gives me the pixel data for a corresponding pixel in the upper-left, but tapping on a pixel in the lower-left corner returns the correct pixel colour). The touch coordinates (CGPoint) are correct.

What am I doing wrong?

Here’s my code:

@interface UIImageView (PixelColor)
- (UIColor*)getRGBPixelColorAtPoint:(CGPoint)point;
@end

@implementation UIImageView (PixelColor)

- (UIColor*)getRGBPixelColorAtPoint:(CGPoint)point
{
    UIColor* color = nil;

    CGImageRef cgImage = [self.image CGImage];
    size_t width = CGImageGetWidth(cgImage);
    size_t height = CGImageGetHeight(cgImage);
    NSUInteger x = (NSUInteger)floor(point.x);
    NSUInteger y = height - (NSUInteger)floor(point.y);

    if ((x < width) && (y < height))
    {
        CGDataProviderRef provider = CGImageGetDataProvider(cgImage);
        CFDataRef bitmapData = CGDataProviderCopyData(provider);
        const UInt8* data = CFDataGetBytePtr(bitmapData);
        size_t offset = ((width * y) + x) * 4;
        UInt8 red = data[offset];
        UInt8 blue = data[offset+1];
        UInt8 green = data[offset+2];
        UInt8 alpha = data[offset+3];
        CFRelease(bitmapData);
        color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f];
    }

    return color;
}
  • 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-15T09:12:34+00:00Added an answer on May 15, 2026 at 9:12 am

    I think R B G is wrong. You have:

    UInt8 red =   data[offset];     
    UInt8 blue =  data[offset+1];
    UInt8 green = data[offset+2];
    

    But don’t you really mean R G B? :

    UInt8 red =   data[offset];     
    UInt8 green = data[offset+1];
    UInt8 blue =  data[offset+2];
    

    But even with that fixed there’s still a problem as it turns out Apple byte swaps (great article) the R and B values when on the device, but not when on the simulator.

    I had a similar simulator/device issue with a PNG’s pixel buffer returned by CFDataGetBytePtr.

    This resolved the issue for me:

    #if TARGET_IPHONE_SIMULATOR
            UInt8 red =   data[offset];
            UInt8 green = data[offset + 1];
            UInt8 blue =  data[offset + 2];
    #else
            //on device
            UInt8 blue =  data[offset];       //notice red and blue are swapped
            UInt8 green = data[offset + 1];
            UInt8 red =   data[offset + 2];
    #endif
    

    Not sure if this will fix your issue, but your misbehaving code looks close to what mine looked like before I fixed it.

    One last thing: I believe the simulator will let you access your pixel buffer data[] even after CFRelease(bitmapData) is called. On the device this is not the case in my experience. Your code shouldn’t be affected, but in case this helps someone else I thought I’d mention it.

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

Sidebar

Related Questions

Based on the response to this question: Why does C++ have header files and
Based on this question it appears that the default template for CheckStyle will allow
Based on my previous question here my new concern is how do I unit
Based on this question I don't want to litter my ready stuff waiting for
What is the correct way to convert ASP.NET SOAP-based web services to JSON-based responses?
I have a resource handler that is Response.WriteFile(fileName) based on a parameter passed through
Based on a simple test I ran, I don't think it's possible to put
Based on a few posts I've read concerning version control, it seems people think
Based on all my reading there should be one GC thread to invoke all
Based on their work, how do you distinguish a great SQL developer? Examples might

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.