Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a CGImageRef from a file?

So I know you can get a CGImage from a file using UIImage...

UIImage *img = [UIImage imageNamed:@"name.bmp"];
[img CGImage];

But, is there a way to get a CGImageRef without using a UIImage?

(I am trying this in a static library which doesn't have access to UIKit) I could use other frameworks if necessary, just not UIKit. Would CIImage work? Or NSBitmapImageRep?

like image 799
Ovi Tisler Avatar asked May 07 '10 18:05

Ovi Tisler


1 Answers

You can get it from NSData:

CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:@""]);
CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault); // Or JPEGDataProvider

You would have to be certain that the data was a JPEG or a PNG for this to work.

like image 145
Tom Irving Avatar answered Oct 04 '22 21:10

Tom Irving