diff options
Diffstat (limited to 'platform/macos/src/NSImage+MGLAdditions.mm')
-rw-r--r-- | platform/macos/src/NSImage+MGLAdditions.mm | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/platform/macos/src/NSImage+MGLAdditions.mm b/platform/macos/src/NSImage+MGLAdditions.mm index 2c4b16800e..72ddec83f7 100644 --- a/platform/macos/src/NSImage+MGLAdditions.mm +++ b/platform/macos/src/NSImage+MGLAdditions.mm @@ -2,6 +2,17 @@ @implementation NSImage (MGLAdditions) +- (nullable instancetype)initWithMGLSpriteImage:(const mbgl::SpriteImage *)spriteImage { + std::string png = encodePNG(spriteImage->image); + NSData *data = [[NSData alloc] initWithBytes:png.data() length:png.size()]; + NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:data]; + if ([self initWithSize:NSMakeSize(spriteImage->getWidth(), spriteImage->getHeight())]) { + [self addRepresentation:rep]; + [self setTemplate:spriteImage->sdf]; + } + return self; +} + - (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage { // Create a bitmap image representation from the image, respecting backing // scale factor and any resizing done on the image at runtime. @@ -10,13 +21,11 @@ NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:{ NSZeroPoint, self.size }]; [self unlockFocus]; - // Get the image’s raw pixel data as an RGBA buffer. - std::string pixelString((const char *)rep.bitmapData, rep.pixelsWide * rep.pixelsHigh * 4 /* RGBA */); - mbgl::PremultipliedImage cPremultipliedImage({ static_cast<uint32_t>(rep.pixelsWide), static_cast<uint32_t>(rep.pixelsHigh) }); std::copy(rep.bitmapData, rep.bitmapData + cPremultipliedImage.bytes(), cPremultipliedImage.data.get()); return std::make_unique<mbgl::SpriteImage>(std::move(cPremultipliedImage), - (float)(rep.pixelsWide / self.size.width)); + (float)(rep.pixelsWide / self.size.width), + [self isTemplate]); } @end |