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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:50:02+00:00 2026-05-23T18:50:02+00:00

I am having difficulty with images in my iPhone app. I load an image

  • 0

I am having difficulty with images in my iPhone app. I load an image as follows:

UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@""]];

CGImageRef imageRef = image.CGImage;
NSInteger texWidth = CGImageGetWidth(*imageRef);
NSInteger texHeight = CGImageGetHeight(*imageRef);
GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);
CGContextRef textureContext = CGBitmapContextCreate(textureData, texWidth, texHeight, 8,
                                          texWidth * 4, CGImageGetColorSpace(*imageRef),
                                          kCGImageAlphaPremultipliedLast);


CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)texWidth,
                                              (float)texHeight), *imageRef);
CGContextRelease(textureContext);

glBindTexture(GL_TEXTURE_2D, location);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA,
                                       GL_UNSIGNED_BYTE, textureData);

free(textureData);

and I want to free all the memory that the image has used. I attempt to do that as follows:

CGImageRelease(imageRef);
free(image);

However, the memory does not appear to be freed according to Instruments. Am I missing something? Is it being cached by one of these functions? Furthermore, when I reload the image later, it appears to be loaded twice, i.e. the alpha channel is twice as much as it was the first time. And again, when I (attempt to) free the memory and come to reload it for a third time, the alpha channel is three times what it was the first time. The parts that are supposed to be almost transparent are becoming more and more opaque.

The issue with the transparency was not noticed in iOS 3, only iOS 4. But the issue with the memory not being freed was noticed in both.

Please can someone help me with this? I’ve spent so much time messing around with images! Also, I don’t think I use a stupid amount of images: I am making a game that uses about 40 images, most of which are 128×128 PNGs. How come I run out of memory so quickly?? What is the “common” practice for this scenario? Am I right in trying to free the memory when the image isn’t needed?

Replies will be greatly appreciated! Thank you.

  • 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-23T18:50:03+00:00Added an answer on May 23, 2026 at 6:50 pm

    I think that you should do:

    [image release];
    

    instead of

    CGImageRelease(imageRef);
    

    In fact the object that you are allocating is image, then you get a reference to one of its members; you do not need to release the reference to the member, but to the original object you allocated. You do not need calling free(*image);

    EDIT:

    All your usage of *image and *imageRef does not seem correct to me; you should use instead image and imageRef:

    CGImageRef imageRef = image.CGImage;
    NSInteger texWidth = CGImageGetWidth(imageRef);
    NSInteger texHeight = CGImageGetHeight(imageRef);
    GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);
    CGContextRef textureContext = CGBitmapContextCreate(textureData, texWidth, texHeight, 8,
                                          texWidth * 4, CGImageGetColorSpace(imageRef),
                                          kCGImageAlphaPremultipliedLast);
    
    CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)texWidth,
                                              (float)texHeight), imageRef);
    CGContextRelease(textureContext);
    
    glBindTexture(GL_TEXTURE_2D, location);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA,
                                       GL_UNSIGNED_BYTE, textureData);
    
    free(textureData);
    

    About memory consumption: I think that using the OpenGLES framework might exact a fixed penalty, i.e. increase the overall memory footprint of your app (due to loading the framework and instantiating I don’t know which objects); but you should not experience an unexpected memory increase each time that you draw a texture.

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

Sidebar

Related Questions

I'm having a lot of difficulty matching an image url with spaces. I need
I'm having difficulty getting my footer image in Internet Explorer 8 to display on
I'm having difficulty moving an image to another location after the first animation is
I'm having some difficulty embedding an image from the Properties.Resources to a MailMessage, currently
I am having difficulty in changing the image of my UIButton which is contained
I'm having difficulty with a jquery animation. When the image slider selects the next
I am having a real difficulty. I have a number of images with a
I am having difficulty creating the image icon. I want to package a jar
I'm having real difficulty pulling all atttachments including the image... The thing is, I
I have an app with a widget but I am having some difficulty with

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.