[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
This commit is contained in:
Roman Blum
2016-10-12 21:30:20 +02:00
committed by Minh Nguyễn
parent 7bbf4a286e
commit a24e559ac8
11 changed files with 515 additions and 408 deletions

View File

@@ -12,14 +12,14 @@ NS_ASSUME_NONNULL_BEGIN
A version number identifying the default version of the suite of default styles
provided by Mapbox. This version number may be passed into one of the
“StyleURLWithVersion” class methods of MGLStyle.
The value of this constant generally corresponds to the latest released version
as of the date on which this SDK was published. You can use this constant to
ascertain the style used by `MGLMapView` and `MGLTilePyramidOfflineRegion` when
no style URL is specified. Consult the
<a href="https://www.mapbox.com/api-documentation/#styles">Mapbox Styles API documentation</a>
for the most up-to-date style versioning information.
@warning The value of this constant may change in a future release of the SDK.
If you use any feature that depends on a specific aspect of a default style
– for instance, the minimum zoom level that includes roads – you may use the
@@ -30,8 +30,8 @@ NS_ASSUME_NONNULL_BEGIN
static const NSInteger MGLStyleDefaultVersion = 9;
/**
The proxy object for the current map style for customization purposes and a
set of convenience methods for creating style URLs of default styles provided
The proxy object for the current map style for customization purposes and a
set of convenience methods for creating style URLs of default styles provided
by Mapbox.
<a href="https://www.mapbox.com/maps/">Learn more about Mapbox default styles</a>.
*/
@@ -42,9 +42,9 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to version 8 of the
<a href="https://www.mapbox.com/maps/streets/">Mapbox Streets</a> style.
Streets is a general-purpose style with detailed road and transit networks.
`MGLMapView` and `MGLTilePyramidOfflineRegion` use Mapbox Streets when no style
is specified explicitly.
*/
@@ -53,12 +53,12 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to the given version of the
<a href="https://www.mapbox.com/maps/streets/">Mapbox Streets</a> style.
Streets is a general-purpose style with detailed road and transit networks.
`MGLMapView` and `MGLTilePyramidOfflineRegion` use Mapbox Streets when no style
is specified explicitly.
@param version The style’s latest released version. As of publication, the
current version is `9`.
*/
@@ -67,7 +67,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to version 8 of the
<a href="https://www.mapbox.com/blog/emerald-gl/">Mapbox Emerald</a> style.
Emerald is a tactile style with subtle textures and dramatic hillshading.
*/
+ (NSURL *)emeraldStyleURL __attribute__((deprecated("Create an NSURL object with the string “mapbox://styles/mapbox/emerald-v8”.")));
@@ -75,9 +75,9 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to the given version of the
<a href="https://www.mapbox.com/maps/outdoors/">Mapbox Outdoors</a> style.
Outdoors is a general-purpose style tailored to outdoor activities.
@param version The style’s latest released version. As of publication, the
current version is `9`.
*/
@@ -86,7 +86,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to version 8 of the
<a href="https://www.mapbox.com/maps/light-dark/">Mapbox Light</a> style.
Light is a subtle, light-colored backdrop for data visualizations.
*/
+ (NSURL *)lightStyleURL __attribute__((deprecated("Use -lightStyleURLWithVersion:.")));
@@ -94,9 +94,9 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to the given version of the
<a href="https://www.mapbox.com/maps/light-dark/">Mapbox Light</a> style.
Light is a subtle, light-colored backdrop for data visualizations.
@param version The style’s latest released version. As of publication, the
current version is `9`.
*/
@@ -105,7 +105,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to version 8 of the
<a href="https://www.mapbox.com/maps/light-dark/">Mapbox Dark</a> style.
Dark is a subtle, dark-colored backdrop for data visualizations.
*/
+ (NSURL *)darkStyleURL __attribute__((deprecated("Use -darkStyleURLWithVersion:.")));
@@ -113,9 +113,9 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to the given version of the
<a href="https://www.mapbox.com/maps/light-dark/">Mapbox Dark</a> style.
Dark is a subtle, dark-colored backdrop for data visualizations.
@param version The style’s latest released version. As of publication, the
current version is `9`.
*/
@@ -124,7 +124,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to version 8 of the
<a href="https://www.mapbox.com/maps/satellite/">Mapbox Satellite</a> style.
Satellite is high-resolution satellite and aerial imagery.
*/
+ (NSURL *)satelliteStyleURL __attribute__((deprecated("Use -satelliteStyleURLWithVersion:.")));
@@ -132,9 +132,9 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns the URL to the given version of the
<a href="https://www.mapbox.com/maps/satellite/">Mapbox Satellite</a> style.
Satellite is high-resolution satellite and aerial imagery.
@param version The style’s latest released version. As of publication, the
current version is `9`.
*/
@@ -144,7 +144,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
Returns the URL to version 8 of the
<a href="https://www.mapbox.com/maps/satellite/">Mapbox Satellite Streets</a>
style.
Satellite Streets combines the high-resolution satellite and aerial imagery of
Mapbox Satellite with unobtrusive labels and translucent roads from Mapbox
Streets.
@@ -155,11 +155,11 @@ static const NSInteger MGLStyleDefaultVersion = 9;
Returns the URL to the given version of the
<a href="https://www.mapbox.com/maps/satellite/">Mapbox Satellite Streets</a>
style.
Satellite Streets combines the high-resolution satellite and aerial imagery of
Mapbox Satellite with unobtrusive labels and translucent roads from Mapbox
Streets.
@param version The style’s latest released version. As of publication, the
current version is `9`.
*/
@@ -167,7 +167,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
The name of the style.
You can customize the style’s name in Mapbox Studio.
*/
@property (readonly, copy, nullable) NSString *name;
@@ -177,7 +177,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns a layer that conforms to `MGLStyleLayer` if any layer with the given
identifier was found.
@return An instance of a concrete subclass of `MGLStyleLayer` associated with
the given identifier.
*/
@@ -186,7 +186,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns a source if any source with the given identifier was found.
@return An instance of a concrete subclass of `MGLSource` associated with the
given identifier.
*/
@@ -194,7 +194,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Adds a new layer on top of existing layers.
@param layer The layer object to add to the map view. This object must be an
instance of a concrete subclass of `MGLStyleLayer`.
*/
@@ -202,7 +202,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Inserts a new layer below another layer.
@param layer Layer to be inserted.
@param belowLayer A layer that's already on the map view.
*/
@@ -210,7 +210,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Removes a layer from the map view.
@param layer The layer object to remove from the map view. This object
must conform to the `MGLStyleLayer` protocol.
*/
@@ -218,14 +218,14 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Adds a new source to the map view.
@param source The source to add to the map view.
*/
- (void)addSource:(MGLSource *)source;
/**
Removes a source from the map view.
@param source The source to remove.
*/
- (void)removeSource:(MGLSource *)source;
@@ -238,7 +238,7 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Returns a Boolean value indicating whether the style class with the given
identifier is currently active.
@param styleClass The style class to query for.
@return Whether the style class is currently active.
*/
@@ -246,18 +246,36 @@ static const NSInteger MGLStyleDefaultVersion = 9;
/**
Activates the style class with the given identifier.
@param styleClass The style class to activate.
*/
- (void)addStyleClass:(NSString *)styleClass;
/**
Deactivates the style class with the given identifier.
@param styleClass The style class to deactivate.
*/
- (void)removeStyleClass:(NSString *)styleClass;
/**
Adds or overrides an image used by the style’s layers.
To use an image in a style layer, give it a unique name using this method,
then set the `iconImage` property of an `MGLSymbolStyleLayer` object to that name.
@param image The image for the name.
@param name The name of the image to set to the style.
*/
- (void)setImage:(MGLImage *)image forName:(NSString *)name;
/**
Removes a name and its associated image from the style.
@param name The name of the image to remove.
*/
- (void)removeImageForName:(NSString *)name;
@end
NS_ASSUME_NONNULL_END

View File

@@ -21,6 +21,7 @@
#import "MGLGeoJSONSource.h"
#include <mbgl/util/default_styles.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/style/layers/fill_layer.hpp>
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
@@ -32,6 +33,12 @@
#include <mbgl/style/sources/raster_source.hpp>
#include <mbgl/mbgl.hpp>
#if TARGET_OS_IPHONE
#import "UIImage+MGLAdditions.h"
#else
#import "NSImage+MGLAdditions.h"
#endif
@interface MGLStyle()
@property (nonatomic, weak) MGLMapView *mapView;
@property (readonly, copy, nullable) NSURL *URL;
@@ -103,7 +110,7 @@ static NSURL *MGLStyleURL_emerald;
if (!mbglLayer) {
return nil;
}
MGLStyleLayer *styleLayer;
if (auto fillLayer = mbglLayer->as<mbgl::style::FillLayer>()) {
MGLSource *source = [self sourceWithIdentifier:@(fillLayer->getSourceID().c_str())];
@@ -126,9 +133,9 @@ static NSURL *MGLStyleURL_emerald;
NSAssert(NO, @"Unrecognized layer type");
return nil;
}
styleLayer.layer = mbglLayer;
return styleLayer;
}
@@ -138,7 +145,7 @@ static NSURL *MGLStyleURL_emerald;
if (!mbglSource) {
return nil;
}
// TODO: Fill in options specific to the respective source classes
// https://github.com/mapbox/mapbox-gl-native/issues/6584
MGLSource *source;
@@ -152,9 +159,9 @@ static NSURL *MGLStyleURL_emerald;
NSAssert(NO, @"Unrecognized source type");
return nil;
}
source.source = mbglSource;
return source;
}
@@ -171,7 +178,7 @@ static NSURL *MGLStyleURL_emerald;
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
NSStringFromClass(self)];
}
self.mapView.mbglMap->addLayer(std::unique_ptr<mbgl::style::Layer>(layer.layer));
}
@@ -191,7 +198,7 @@ static NSURL *MGLStyleURL_emerald;
@"Make sure the style layer was created as a member of a concrete subclass of MGLStyleLayer.",
NSStringFromClass(otherLayer)];
}
const mbgl::optional<std::string> belowLayerId{otherLayer.identifier.UTF8String};
self.mapView.mbglMap->addLayer(std::unique_ptr<mbgl::style::Layer>(layer.layer), belowLayerId);
}
@@ -209,13 +216,13 @@ static NSURL *MGLStyleURL_emerald;
- (NS_ARRAY_OF(NSString *) *)styleClasses
{
const std::vector<std::string> &appliedClasses = self.mapView.mbglMap->getClasses();
NSMutableArray *returnArray = [NSMutableArray arrayWithCapacity:appliedClasses.size()];
for (auto appliedClass : appliedClasses) {
[returnArray addObject:@(appliedClass.c_str())];
}
return returnArray;
}
@@ -227,12 +234,12 @@ static NSURL *MGLStyleURL_emerald;
- (void)setStyleClasses:(NS_ARRAY_OF(NSString *) *)appliedClasses transitionDuration:(NSTimeInterval)transitionDuration
{
std::vector<std::string> newAppliedClasses;
for (NSString *appliedClass in appliedClasses)
{
newAppliedClasses.push_back([appliedClass UTF8String]);
}
mbgl::style::TransitionOptions transition { { MGLDurationInSeconds(transitionDuration) } };
self.mapView.mbglMap->setTransitionOptions(transition);
self.mapView.mbglMap->setClasses(newAppliedClasses);
@@ -259,6 +266,21 @@ static NSURL *MGLStyleURL_emerald;
}
}
- (void)setImage:(MGLImage *)image forName:(NSString *)name
{
NSAssert(image, @"image is null");
NSAssert(name, @"name is null");
self.mapView.mbglMap->addImage([name UTF8String], image.mgl_spriteImage);
}
- (void)removeImageForName:(NSString *)name
{
NSAssert(name, @"name is null");
self.mapView.mbglMap->removeImage([name UTF8String]);
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; name = %@, URL = %@>",

View File

@@ -2,6 +2,14 @@
#pragma once
#if TARGET_OS_IPHONE
@class UIImage;
#define MGLImage UIImage
#else
@class NSImage;
#define MGLImage NSImage
#endif
#if TARGET_OS_IPHONE
@class UIColor;
#define MGLColor UIColor

View File

@@ -7,6 +7,10 @@
objects = {
/* Begin PBXBuildFile section */
30E578171DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */; };
30E578181DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */; };
30E578191DAA855E0050F07E /* UIImage+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 30E578121DAA7D690050F07E /* UIImage+MGLAdditions.mm */; };
30E5781A1DAA855E0050F07E /* UIImage+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 30E578121DAA7D690050F07E /* UIImage+MGLAdditions.mm */; };
350098AF1D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 350098AD1D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
350098B01D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 350098AD1D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
350098B11D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 350098AE1D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.mm */; };
@@ -494,6 +498,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+MGLAdditions.h"; path = "src/UIImage+MGLAdditions.h"; sourceTree = SOURCE_ROOT; };
30E578121DAA7D690050F07E /* UIImage+MGLAdditions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "UIImage+MGLAdditions.mm"; path = "src/UIImage+MGLAdditions.mm"; sourceTree = SOURCE_ROOT; };
350098AD1D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIColor+MGLStyleAttributeAdditions.h"; path = "src/UIColor+MGLStyleAttributeAdditions.h"; sourceTree = SOURCE_ROOT; };
350098AE1D47E6F4004B2AF0 /* UIColor+MGLStyleAttributeAdditions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "UIColor+MGLStyleAttributeAdditions.mm"; path = "src/UIColor+MGLStyleAttributeAdditions.mm"; sourceTree = SOURCE_ROOT; };
350098B91D480108004B2AF0 /* MGLVectorSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLVectorSource.h; sourceTree = "<group>"; };
@@ -939,6 +945,8 @@
children = (
35CE61801D4165D9004F2359 /* UIColor+MGLAdditions.h */,
35CE61811D4165D9004F2359 /* UIColor+MGLAdditions.mm */,
30E578111DAA7D690050F07E /* UIImage+MGLAdditions.h */,
30E578121DAA7D690050F07E /* UIImage+MGLAdditions.mm */,
);
name = Categories;
sourceTree = "<group>";
@@ -1473,6 +1481,7 @@
DA8847F81CBAFA5100AB86E3 /* MGLPointAnnotation.h in Headers */,
353933F21D3FB753003F57D7 /* MGLCircleStyleLayer.h in Headers */,
DA8847F31CBAFA5100AB86E3 /* MGLMultiPoint.h in Headers */,
30E578171DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */,
DAD1656C1CF41981001FF4B9 /* MGLFeature.h in Headers */,
40EDA1C01CFE0E0200D9EA68 /* MGLAnnotationContainerView.h in Headers */,
DA88484F1CBAFB9800AB86E3 /* MGLAnnotationImage_Private.h in Headers */,
@@ -1564,6 +1573,7 @@
353933F31D3FB753003F57D7 /* MGLCircleStyleLayer.h in Headers */,
3593E5271D529EDC006D9365 /* UIColor+MGLStyleAttributeAdditions_Private.h in Headers */,
3538AA1E1D542239008EC33D /* MGLForegroundStyleLayer.h in Headers */,
30E578181DAA85520050F07E /* UIImage+MGLAdditions.h in Headers */,
40F887711D7A1E59008ECB67 /* MGLGeoJSONSource_Private.h in Headers */,
DABFB8631CBE99E500D62B32 /* MGLOfflineRegion.h in Headers */,
DA35A2B21CCA141D00E826B2 /* MGLCompassDirectionFormatter.h in Headers */,
@@ -1930,6 +1940,7 @@
DA88485D1CBAFB9800AB86E3 /* MGLFaux3DUserLocationAnnotationView.m in Sources */,
DAD165701CF41981001FF4B9 /* MGLFeature.mm in Sources */,
350098C31D48149E004B2AF0 /* NSNumber+MGLStyleAttributeAdditions.mm in Sources */,
30E578191DAA855E0050F07E /* UIImage+MGLAdditions.mm in Sources */,
40EDA1C11CFE0E0500D9EA68 /* MGLAnnotationContainerView.m in Sources */,
DA8848541CBAFB9800AB86E3 /* MGLCompactCalloutView.m in Sources */,
DA8848251CBAFA6200AB86E3 /* MGLPointAnnotation.m in Sources */,
@@ -2000,6 +2011,7 @@
DAED38661D62D0FC00D7640F /* NSURL+MGLAdditions.m in Sources */,
DAD165711CF41981001FF4B9 /* MGLFeature.mm in Sources */,
350098C41D48149E004B2AF0 /* NSNumber+MGLStyleAttributeAdditions.mm in Sources */,
30E5781A1DAA855E0050F07E /* UIImage+MGLAdditions.mm in Sources */,
40EDA1C21CFE0E0500D9EA68 /* MGLAnnotationContainerView.m in Sources */,
DAA4E4291CBB730400178DFB /* NSBundle+MGLAdditions.m in Sources */,
DAA4E42E1CBB730400178DFB /* MGLAPIClient.m in Sources */,

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,9 @@
#import <UIKit/UIKit.h>
#include <mbgl/sprite/sprite_image.hpp>
@interface UIImage (MGLAdditions)
- (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage;
@end

View File

@@ -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

View File

@@ -7,6 +7,8 @@
objects = {
/* Begin PBXBuildFile section */
30E5781B1DAA857E0050F07E /* NSImage+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 30E578141DAA7D920050F07E /* NSImage+MGLAdditions.h */; };
30E5781C1DAA85820050F07E /* NSImage+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 30E578151DAA7D920050F07E /* NSImage+MGLAdditions.mm */; };
3508EC641D749D39009B0EE4 /* NSExpression+MGLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 3508EC621D749D39009B0EE4 /* NSExpression+MGLAdditions.h */; };
3508EC651D749D39009B0EE4 /* NSExpression+MGLAdditions.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3508EC631D749D39009B0EE4 /* NSExpression+MGLAdditions.mm */; };
352742781D4C220900A1ECE6 /* MGLStyleAttributeValue.h in Headers */ = {isa = PBXBuildFile; fileRef = 352742771D4C220900A1ECE6 /* MGLStyleAttributeValue.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -217,6 +219,8 @@
/* End PBXCopyFilesBuildPhase section */
/* Begin PBXFileReference section */
30E578141DAA7D920050F07E /* NSImage+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSImage+MGLAdditions.h"; path = "src/NSImage+MGLAdditions.h"; sourceTree = SOURCE_ROOT; };
30E578151DAA7D920050F07E /* NSImage+MGLAdditions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = "NSImage+MGLAdditions.mm"; path = "src/NSImage+MGLAdditions.mm"; sourceTree = SOURCE_ROOT; };
3508EC621D749D39009B0EE4 /* NSExpression+MGLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSExpression+MGLAdditions.h"; sourceTree = "<group>"; };
3508EC631D749D39009B0EE4 /* NSExpression+MGLAdditions.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "NSExpression+MGLAdditions.mm"; sourceTree = "<group>"; };
352742771D4C220900A1ECE6 /* MGLStyleAttributeValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MGLStyleAttributeValue.h; sourceTree = "<group>"; };
@@ -713,6 +717,8 @@
DAD1657F1CF4CF50001FF4B9 /* Categories */ = {
isa = PBXGroup;
children = (
30E578141DAA7D920050F07E /* NSImage+MGLAdditions.h */,
30E578151DAA7D920050F07E /* NSImage+MGLAdditions.mm */,
DAE6C37D1CC31E2A00DB3429 /* NSBundle+MGLAdditions.h */,
DAE6C37E1CC31E2A00DB3429 /* NSBundle+MGLAdditions.m */,
35C5D8431D6DD66D00E95907 /* NSComparisonPredicate+MGLAdditions.h */,
@@ -887,6 +893,7 @@
DA8F25AE1D51CB270010E6B5 /* NSArray+MGLStyleAttributeAdditions_Private.h in Headers */,
DAE6C3991CC31E2A00DB3429 /* NSException+MGLAdditions.h in Headers */,
DA8F25871D51C9E10010E6B5 /* MGLBackgroundStyleLayer.h in Headers */,
30E5781B1DAA857E0050F07E /* NSImage+MGLAdditions.h in Headers */,
DAE6C3661CC31E0400DB3429 /* MGLShape.h in Headers */,
352742811D4C243B00A1ECE6 /* MGLSource.h in Headers */,
DAE6C3C21CC31F4500DB3429 /* Mapbox.h in Headers */,
@@ -1141,6 +1148,7 @@
DAE6C38A1CC31E2A00DB3429 /* MGLMultiPoint.mm in Sources */,
DAE6C3961CC31E2A00DB3429 /* MGLTypes.m in Sources */,
DA35A2A61CC9EB2700E826B2 /* MGLCoordinateFormatter.m in Sources */,
30E5781C1DAA85820050F07E /* NSImage+MGLAdditions.mm in Sources */,
352742821D4C243B00A1ECE6 /* MGLSource.mm in Sources */,
DAE6C3881CC31E2A00DB3429 /* MGLMapCamera.mm in Sources */,
DA6408D81DA4E5DA00908C90 /* MGLVectorStyleLayer.m in Sources */,

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,9 @@
#import <Cocoa/Cocoa.h>
#include <mbgl/sprite/sprite_image.hpp>
@interface NSImage (MGLAdditions)
- (std::unique_ptr<mbgl::SpriteImage>)mgl_spriteImage;
@end

View File

@@ -0,0 +1,23 @@
#import "NSImage+MGLAdditions.h"
@implementation NSImage (MGLAdditions)
- (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.
// http://www.cocoabuilder.com/archive/cocoa/82430-nsimage-getting-raw-bitmap-data.html#82431
[image lockFocus];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:{ NSZeroPoint, self.size }];
[image 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(rep.pixelsWide, rep.pixelsHigh);
std::copy(rep.bitmapData, rep.bitmapData + cPremultipliedImage.size(), cPremultipliedImage.data.get());
return std::make_unique<mbgl::SpriteImage>(std::move(cPremultipliedImage),
(float)(rep.pixelsWide / self.size.width));
}
@end