Add nativeImage.createFromNamedImage API

This commit is contained in:
Samuel Attard 2017-10-10 03:13:40 +11:00
parent c85b159d46
commit e027ba9c47
3 changed files with 31 additions and 0 deletions

View file

@ -6,10 +6,30 @@
#import <Cocoa/Cocoa.h>
#include "base/strings/sys_string_conversions.h"
namespace atom {
namespace api {
mate::Handle<NativeImage> NativeImage::CreateFromNamedImage(
mate::Arguments* args, const std::string& name) {
@autoreleasepool {
NSImage* image = [NSImage imageNamed:base::SysUTF8ToNSString(name)];
if (!image.valid) {
args->ThrowError("Cannot create image from name: " + name);
return CreateEmpty(args->isolate());
}
CGImageRef ref = [image CGImageForProposedRect:nil context:nil hints:nil];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCGImage:ref];
[rep setSize:[image size]];
NSData* pngData = [rep representationUsingType:NSPNGFileType properties:[[NSDictionary alloc] init]];
return CreateFromPNG(args->isolate(), (char *) [pngData bytes], [pngData length]);
}
}
void NativeImage::SetTemplateImage(bool setAsTemplate) {
[image_.AsNSImage() setTemplate:setAsTemplate];
}