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

  • Home
  • SEARCH
  • 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 5986463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:40:31+00:00 2026-05-22T22:40:31+00:00

I’m using CGDataProviderCreateWithData to (eventually) create a UIImage from a malloced array of bytes.

  • 0

I’m using CGDataProviderCreateWithData to (eventually) create a UIImage from a malloced array of bytes. I call CGDataProviderCreateWithData like this:

 provider = CGDataProviderCreateWithData(NULL, dataPtr, dataLen, callbackFunc);

where

dataPtr is the previously malloced array of data bytes for the image,
dataLen is the number of bytes in the dataPtr array, and
callbackFunc is as described in the CGDataProviderCreateWithData documentation:

 void callbackFunc(void *info, const void *data, size_t size);

The callback function is called when the data provider is released so I could free() dataPtr there, but I may want to continue using it (dataPtr) and at some later stage free it. This block of code will be called multiple times, and the flow will look something like:

  1. malloc(dataPtr)
  2. create image (call CGDataProviderCreateWithData etc)
  3. display image
  4. release image (and so release data provider created by CGDataProviderCreateWithData)
  5. continue to use dataPtr
  6. free(dataPtr)

so 1..6 may be executed multiple times. I don’t want dataPtr hanging around for the entire execution of the program (and it may change in size anyway), so I want to malloc/free it as necessary.

The problem is that I can’t free(dataPtr) in the callback from CGDataProviderCreateWithData because I still want to use it, so I want to free it some time later – and I can’t free it until I know that the data provider no longer needs it (as far as I can tell CGDataProviderCreateWithData uses the array I pass, it doesn’t take a copy).

I can’t do (1) above until I know it is ok to free and re-malloc dataPtr, so what I really want to do is block waiting for the data provider to be freed (well, I want to know whether I should re-enter the 1..6 block of code, which I can’t do until the data provider is freed). It will be – I create the data provider, create the image and immediately display it and release the data provider. The trouble is that the data provider isn’t actually released until the UIImage is released and is finished with it.

I’m reasonably new to objective-c and iOS. Am I missing something obvious?

  • 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-22T22:40:31+00:00Added an answer on May 22, 2026 at 10:40 pm

    What I’m doing is essentially the same to what you want to achieve, I just took different route.

    This whole process is wrapped into NSOperation so you have control over scheduling memory usage.

    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
    
    NSUInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
    
    
    UInt8 *rawData = calloc((height * width * bitsPerComponent), sizeof(UInt8));
    
    NSUInteger bytesPerRow = CGImageGetBytesPerRow(imageRef);
    
    
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                                bitsPerComponent, bytesPerRow, colorSpace,
                                                kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
    
    if (context)
    {   
    
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
                // This is the place you get access to data trough rawData. Do whatever you want with it, when done, get image with 
     CGImageRef newImg = CGBitmapContextCreateImage(context);
     }
    

    OR

    subclass CALayer, override drawInContext and use CGBitmapContextGetData to get raw data. Don’t have much experience with this though, sorry, but if you want to see instant changes
    on image based on user input, I’d do it this way:

    Subclass UIView and CALayer which becomes layer for view and displays image. View gets input and image data (from CGBitmapContextGetData) in layer class is manipulated based on input.
    Even CATiledLayer can be used for huge images in this way. When done with image, just release UIView and replace it with new one. I’ll gladly provide help if you need any, just ping here.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
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
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.