Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly to make a CGImageRef from an image on disk

I've looked around everywhere to no avail. I'm doing some image loading in a thread and since UIKit is not thread safe I'm going to have to store the images as CGImageRefs but I can't figure out how to do this. I haven't played with any of the Quartz stuff before so it's confusing me. Basically I just need to load a JPG from the disk into a CGImageRef. Also for bonus points, is there anyway to load a GIF into a CGImageRef?

like image 374
Alexander Avatar asked Aug 23 '10 17:08

Alexander


2 Answers

gcamp is more or less correct, you are pretty safe to use this on a background thread, but to answer your question, I believe the following would work.

CGDataProviderRef dataProvider = CGDataProviderCreateWithFilename("file");
CGImageRef image = CGImageCreateWithPNGDataProvider(dataProvider, NULL, NO, kCGRenderingIntentDefault);

There is also a function for jpg images, CGImageCreateWithJPEGDataProvider.

(be sure to release the data provider and image when done)

like image 155
Jerry Jones Avatar answered Nov 17 '22 11:11

Jerry Jones


I think the simplest way is to use UIKit and use the CGImage propriety of UIImage.

Something like that :

UIImage* image = [UIImage imageNamed:@"Your Image.png"];
CGImageRef imageRef = image.CGImage;
like image 21
gcamp Avatar answered Nov 17 '22 11:11

gcamp