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?
Asked
Active
Viewed 1.6k times
2 Answers
30
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)

Mazyod
- 22,319
- 10
- 92
- 157

Jerry Jones
- 5,393
- 2
- 22
- 14
10
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;

gcamp
- 14,622
- 4
- 54
- 85
-
1Okay but the point is, this is happening in a thread and UIKit is not thread safe. Otherwise I'd just send back the UIImage. I need to be able to read an image from a file and directly create a CGImageRef for it – Alexander Aug 23 '10 at 18:04
-
Unfortunately the O.P. is right, that Apple says "All UIKit objects should be used on the main thread only." – Frank Schmitt Aug 23 '10 at 18:04
-
If I'm not mistaken, UI updates should be done on the main thread. Other parts of UIKit are actually thread safe. – gcamp Aug 23 '10 at 18:17
-
Yes UI updates should be done on the main thread, however I'm just LOADING images from disk (which is an expensive process on the UI thread) and no UIImages are not thread safe and thus the CGImageRefs – Alexander Aug 23 '10 at 18:50
-
Actually, UIImages are thread safe, from iOS4-onwards. They are a rare exception to the norm. – Benjamin Mayo Oct 01 '11 at 17:09