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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:32:54+00:00 2026-06-15T04:32:54+00:00

+(UIImage*) createCGImageFromFile:(NSString*)inName { if(!inName || [inName length]==0) return NULL; // use the data provider

  • 0
+(UIImage*) createCGImageFromFile:(NSString*)inName
{
    if(!inName || [inName length]==0)
        return  NULL;

    // use the data provider to get a CGImage; release the data provider
    CGImageRef image= NULL;
    CGFloat scale = 1.0f;
    CGDataProviderRef dataProvider = NULL;

    NSString *fileName = nil;
    NSString *path = nil;
    if([Utilities isRetinaDevice])
    {
        NSString *extenstion = [inName pathExtension];
        NSString *stringWithoutPath = [inName stringByDeletingPathExtension];
        fileName = [NSString stringWithFormat:@"/Images/%@@2x.%@",stringWithoutPath,extenstion];
        path = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:fileName];
        dataProvider = CGDataProviderCreateWithFilename([path UTF8String]);
        if(dataProvider)
        {
            image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO,kCGRenderingIntentDefault);
            CGDataProviderRelease(dataProvider);
            if(image)
            {
                scale = 2.0f;
            }
        }
    }

    if(image == NULL) //Try normal image
    {
        fileName =  [NSString stringWithFormat:@"/Images/%@",inName];
        path = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:fileName];
        dataProvider = CGDataProviderCreateWithFilename([path UTF8String]);
        if(dataProvider)
        {
            image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO,kCGRenderingIntentDefault);
            CGDataProviderRelease(dataProvider);
        }
    }

    // make a bitmap context of a suitable size to draw to, forcing decode
    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);
    unsigned char *imageBuffer = (unsigned char *)malloc(width*height*4);

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef imageContext =
    CGBitmapContextCreate(imageBuffer, width, height, 8, width*4, colourSpace,
                          kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);

    CGColorSpaceRelease(colourSpace);

    // draw the image to the context, release it
    CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image);
    CGImageRelease(image);

    // now get an image ref from the context
    CGImageRef cgImageRef = CGBitmapContextCreateImage(imageContext);

    CGContextRelease(imageContext);
    free(imageBuffer);

    UIImage *outImage = nil;
    if(cgImageRef)
    {
        outImage = [[UIImage alloc] initWithCGImage:cgImageRef scale:scale orientation:UIImageOrientationUp];
        CGImageRelease(cgImageRef);
    }

    return outImage;//Need to release by the receiver
}

Earlier the function was not loading retina images since i was not appending @2x to the image path, i thought that apple itself will take care.
Now I modified the code to load @2x images and created UIImage using scale factor, am I missing something in above code? Is it fine?

P.S. Currently I am not facing any problem using this code but I am not 100% sure

  • 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-15T04:32:55+00:00Added an answer on June 15, 2026 at 4:32 am

    I would use UIImage to load it (it will correct the path)
    That will greatly simply your code

    THEN get a CGImage from it (UIImage is basically just a thin wrapper around CG so thats no real overhead)

    then proceed with your code – it looks ok to me

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

Sidebar

Related Questions

Is the UIImage method: + (UIImage *)imageWithCGImage:(CGImageRef)imageRef scale:(CGFloat)scale orientation:(UIImageOrientation)orientation cropping the original image or
If I use [UIImage imageWithCGImage:] , passing in a CGImageRef , do I then
According to the UIImage documentation : In low-memory situations, image data may be purged
UIImage *image = [[UIImage alloc] initWithData:data]; ((YKSubCategory *)[_subCategories objectAtIndex:connection.tag - 1]).menuImage = image; [image
cell.image = [UIImage imageNamed:@search_up.png]; whenever i'm writing this statement i get warning like deprecated
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]]; I use the code above and then
UIImage *img=[[UIImage alloc]initWithFrame:CGRectMake(50,50,145,20)]; img.image=[UIImage imageNamed:@dollor.png]; I get the error Request for member image in
I want to store UIImage in core data and use the default value transformer.
I have a UIImage (Cocoa Touch). From that, I'm happy to get a CGImage
Should I use UIImage or CGImage ? Png or Jpg ? I've read 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.