summaryrefslogtreecommitdiff
path: root/platform/ios/src/UIImage+MGLAdditions.mm
diff options
context:
space:
mode:
authorRoman Blum <rmnblm@gmail.com>2016-10-12 21:30:20 +0200
committerMinh Nguyễn <mxn@1ec5.org>2016-10-12 12:30:20 -0700
commita24e559ac83a47d19e28290c7ded427adeb303a3 (patch)
tree930c91599bea3b6a524559ba900fb42b8b034f98 /platform/ios/src/UIImage+MGLAdditions.mm
parent7bbf4a286e36ac26e7fa7317bd7942b5163c1188 (diff)
downloadqtlocation-mapboxgl-a24e559ac83a47d19e28290c7ded427adeb303a3.tar.gz
[ios, macos] possibility to set custom images to style (#6637)
* [ios, macos] possibility to set custom images to style * [ios, macos] setImage:forName now supports MGLImage (UIImage/NSImage) * [ios, macos] update documentation for setImage:forName: and removeImageForName: * [ios, macos] rename method and fix prefix * [ios, macos] change header visibility to project * [ios, macos] update header imports * [ios, macos] delete unnecessary whitespaces * [ios, macos] remove unnecessary nil checks * [ios, macos] delete unnecessary whitespaces * [ios, macos] update documentation * [ios, macos] make mgl_spriteImage a parameter-less instance method * [ios, macos] add asserts
Diffstat (limited to 'platform/ios/src/UIImage+MGLAdditions.mm')
-rw-r--r--platform/ios/src/UIImage+MGLAdditions.mm27
1 files changed, 27 insertions, 0 deletions
diff --git a/platform/ios/src/UIImage+MGLAdditions.mm b/platform/ios/src/UIImage+MGLAdditions.mm
new file mode 100644
index 0000000000..8ec8f9e15f
--- /dev/null
+++ b/platform/ios/src/UIImage+MGLAdditions.mm
@@ -0,0 +1,27 @@
+#import "UIImage+MGLAdditions.h"
+
+@implementation UIImage (MGLAdditions)
+
+- (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage
+{
+ CGImageRef cgImage = self.CGImage;
+ size_t width = CGImageGetWidth(cgImage);
+ size_t height = CGImageGetHeight(cgImage);
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ mbgl::PremultipliedImage cPremultipliedImage(width, height);
+ size_t bytesPerPixel = 4;
+ size_t bytesPerRow = bytesPerPixel * width;
+ size_t bitsPerComponent = 8;
+
+ CGContextRef context = CGBitmapContextCreate(cPremultipliedImage.data.get(),
+ width, height, bitsPerComponent, bytesPerRow,
+ colorSpace, kCGImageAlphaPremultipliedLast);
+
+ CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
+ CGContextRelease(context);
+ CGColorSpaceRelease(colorSpace);
+
+ return std::make_unique<mbgl::SpriteImage>(std::move(cPremultipliedImage), float(self.scale));
+}
+
+@end