As of 2021, CALayers
contents can directly be feed with NSImage
but you still may get an
[Utility] unsupported surface format: LA08
warning.
After a couple of days research and testing around i found out that this Warning is triggered if you created an NSView with backingLayer, aka CALayer and just used an NSImage
to feed its contents
. This alone is not much of a problem. But if you try to render it via [self.layer renderInContext:ctx]
, each rendering will trigger the warning again.
To make use simple, i created an NSImage extension to fix this..
@interface NSImage (LA08FIXExtension)
-(id)CGImageRefID;
@end
@implementation NSImage (LA08FIXExtension)
-(id)CGImageRefID {
NSSize size = [self size];
NSRect rect = NSMakeRect(0, 0, size.width, size.height);
CGImageRef ref = [self CGImageForProposedRect:&rect context:[NSGraphicsContext currentContext] hints:NULL];
return (__bridge id)ref;
}
@end
see how it does not return an CGImageRef directly but a bridged cast to id
..! This does the trick.
Now you can use it like..
CALayer *somelayer = [CALayer layer];
somelayer.frame = ...
somelayer.contents = [NSImage imageWithName:@"SomeImage"].CGImageRefID;
PS: posted her because if you got that problem also, google will lead you to this Thread of answers anyway, and all above answers inspired me for this fix.