views:

1487

answers:

1

I am wondering what the most efficient way is to make a CALayer with an image in it.

I know you can load a UIImage and then call [image CGImage] but is this the best way of going about it? As far as I can tell from Apple's documentation this is the only way you can do it.

+1  A: 

Well, a CGImage isn't a CALayer, so you are obviously leaving some steps out there, but I assume you know what you're talking about, as far as drawing to a CALayer or whatnot.

If your question is about creating CGImages without using a UIImage, you can try looking into the following functions:

  • CGImageCreateWithJPEGDataProvider
  • CGImageCreateWithPNGDataProvider

If you happen to know beforehand what sort of image it is, just use the appropriate method. Otherwise, you'd need to look at the file signature to see if it contains PNG or JFIF. Of course, this requires you implement a CGDataProvider.

I assume this is exactly what the UIImage class is already doing. If you want to squeeze out every bit of efficiency from that, you can use one of the following methods in the UIImage class:

+ (UIImage *)imageWithContentsOfFile:(NSString *)path
+ (UIImage *)imageWithData:(NSData *)data

The only difference between these methods and

+ (UIImage *)imageNamed:(NSString *)name

is that imageNamed: caches the image. If you use one of the first methods, it does not.

Ed Marty
Thanks for that. CGImageCreateWithJPEGDataProvider and CGImageCreateWithPNGDataProvider were exactly what I was looking for.
Michael Gaylord