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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:56:04+00:00 2026-06-16T12:56:04+00:00

I am trying to read the ARGB pixel data from a png image asset

  • 0

I am trying to read the ARGB pixel data from a png image asset in my ios App.
I am using CGDataProvider to get a CFDataRef as described here:

http://developer.apple.com/library/ios/#qa/qa1509/_index.html

It works perfectly the first time I use it on a certain image. But the second time I use it on THE SAME image, it returns a length 0 CFDataRef.

Maybe I am not releasing something? Why would it do that?

- (GLuint)initWithCGImage:(CGImageRef)newImageSource
{
    CGDataProviderRef dataProvider;
    CFDataRef dataRef;

    GLuint t;

    @try {
 //   NSLog(@"initWithCGImage");
 //   report_memory2();
    CGFloat widthOfImage = CGImageGetWidth(newImageSource);
    CGFloat heightOfImage = CGImageGetHeight(newImageSource);
    //    pixelSizeOfImage = CGSizeMake(widthOfImage, heightOfImage);
    //    CGSize pixelSizeToUseForTexture = pixelSizeOfImage;

    //    CGSize scaledImageSizeToFitOnGPU = [GPUImageOpenGLESContext sizeThatFitsWithinATextureForSize:pixelSizeOfImage];

    GLubyte *imageData = NULL;
    //CFDataRef dataFromImageDataProvider;


   // stbi stbiClass;
    int x;
    int y;
    int comp;

    dataProvider = CGImageGetDataProvider(newImageSource);
    dataRef = CGDataProviderCopyData(dataProvider);

    const unsigned char * bytesRef = CFDataGetBytePtr(dataRef);
   // NSUInteger length = CFDataGetLength(dataRef);

    //CGDataProviderRelease(dataProvider);
    //dataProvider = nil;
    /*
    UIImage *tmpImage = [UIImage imageWithCGImage:newImageSource];

    NSData *data2 =         UIImagePNGRepresentation(tmpImage);
//    if (data2==NULL)
//        data2 =        UIImageJPEGRepresentation(tmpImage, 1);

    unsigned char *bytes = (unsigned char *)[data2 bytes];
    NSUInteger length = [data2 length];*/
//    stbiClass.img_buffer = bytes;
//    stbiClass.buflen = length;
//    stbiClass.img_buffer_original = bytes;
//    stbiClass.img_buffer_end = bytes + length;

//    unsigned char *data = stbi_load_main(&stbiClass, &x, &y, &comp, 0);
    //unsigned char * data = bytesRef;
    x = widthOfImage;
    y = heightOfImage;
    comp = CGImageGetBitsPerPixel(newImageSource)/8;

    int textureWidth = [self CalcPow2: x];
    int textureHeight = [self CalcPow2: y];

    unsigned char *scaledData = [self scaleImageWithParams:@{@"x":@(x), @"y":@(y), @"comp":@(comp), @"targetX":@(textureWidth), @"targetY":@(textureHeight)} andData:(unsigned char *)bytesRef];
    //CFRelease (dataRef);
   // dataRef = nil;
 //    free (data);

    glGenTextures(1, &t);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, t);
    GLint format  = (comp > 3) ? GL_RGBA : GL_RGB;


    imageData = scaledData;

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexImage2D(GL_TEXTURE_2D, 0, format, textureWidth, textureHeight, 0, format, GL_UNSIGNED_BYTE, imageData);
    //GLenum err = glGetError();
    }
    @finally
    {
        CGDataProviderRelease(dataProvider);
//        CGColorSpaceRelease(colorSpaceRef);
        CGImageRelease(dataRef);
    }


    return t;
}

The second time this is called on a CGImageRef that originate from a [UIimage imageNamed: Path] with the same Path as the first time, I get a dataRef of length 0.
It works the first time though.

  • 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-16T12:56:05+00:00Added an answer on June 16, 2026 at 12:56 pm

    I have found one big issue with the code I posted and fixed it.
    First of all, I was getting crashs even if I didn’t load the same image twice, but rather more images. Since the issue is related to memory it failed in all sort of weird ways.

    The issue with the code is that I am calling: “CGDataProviderRelease(dataProvider);”
    I am using the data provider of newImageSource, but I didn’t create this dataprovider. That is why I shouldn’t release it.
    You need to release things only if you created, retained or copied them.

    Apart from that my App crash sometime due to low memory, but after fixing this I was able to use the “economy” type where I allocate and release as soon as possible.

    Currently I can’t see anything else wrong with this specific code.

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

Sidebar

Related Questions

I am trying to read the ARGB pixels from a an image asset in
When trying to read data from mdb using backgroundworker I get an error The
I am trying to read from a file and encrypt the data using AES
I'm trying to read an xml file on from an android app using XOM
I m trying read XML data using XML parser from Url( https://....etc ). But
I am trying to read an image from /mnt/sdcard/img.jpg into ImageView. Bitmap bm =
Trying to read data from a TcpClient buffer into a byte array: private byte[]
When trying to read a RSA private key from a file using the method
I trying to read a value from an ArrayCollection, I use getItemAt and get
I am trying to read in a scanned image and compress it from a

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.