diff --git a/platform/darwin/test/MGLImageTests.mm b/platform/darwin/test/MGLImageTests.mm index f2334059fa..cb3e0394dc 100644 --- a/platform/darwin/test/MGLImageTests.mm +++ b/platform/darwin/test/MGLImageTests.mm @@ -40,6 +40,12 @@ - (void)testStretching { if (styleImage) { XCTAssert(!styleImage->getContent()); XCTAssertFalse(styleImage->isSdf()); + + MGLImage *imageAfter = [[MGLImage alloc] initWithMGLStyleImage:*styleImage]; + XCTAssertEqual(imageAfter.capInsets.top, 0); + XCTAssertEqual(imageAfter.capInsets.left, 0); + XCTAssertEqual(imageAfter.capInsets.bottom, 0); + XCTAssertEqual(imageAfter.capInsets.right, 0); } } @@ -53,6 +59,12 @@ - (void)testStretching { XCTAssert(styleImage); if (styleImage) { XCTAssert(!styleImage->getContent()); + + MGLImage *imageAfter = [[MGLImage alloc] initWithMGLStyleImage:*styleImage]; + XCTAssertEqual(imageAfter.capInsets.top, 0); + XCTAssertEqual(imageAfter.capInsets.left, 0); + XCTAssertEqual(imageAfter.capInsets.bottom, 0); + XCTAssertEqual(imageAfter.capInsets.right, 0); } } @@ -74,6 +86,12 @@ - (void)testStretching { XCTAssertEqual(content->bottom, 21 * scale); XCTAssertEqual(content->right, 20 * scale); } + + MGLImage *imageAfter = [[MGLImage alloc] initWithMGLStyleImage:*styleImage]; + XCTAssertEqual(imageAfter.capInsets.top, 1); + XCTAssertEqual(imageAfter.capInsets.left, 2); + XCTAssertEqual(imageAfter.capInsets.bottom, 3); + XCTAssertEqual(imageAfter.capInsets.right, 4); } } } diff --git a/platform/ios/src/UIImage+MGLAdditions.mm b/platform/ios/src/UIImage+MGLAdditions.mm index d5ce19cfc7..1f468aa3f2 100644 --- a/platform/ios/src/UIImage+MGLAdditions.mm +++ b/platform/ios/src/UIImage+MGLAdditions.mm @@ -18,12 +18,22 @@ - (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image &)style return nil; } - if (self = [self initWithCGImage:image scale:styleImage.getPixelRatio() orientation:UIImageOrientationUp]) + CGFloat scale = styleImage.getPixelRatio(); + if (self = [self initWithCGImage:image scale:scale orientation:UIImageOrientationUp]) { if (styleImage.isSdf()) { self = [self imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; } + + if (auto content = styleImage.getContent()) + { + UIEdgeInsets capInsets = UIEdgeInsetsMake(content->top / scale, + content->left / scale, + self.size.height - content->bottom / scale, + self.size.width - content->right / scale); + self = [self resizableImageWithCapInsets:capInsets resizingMode:UIImageResizingModeStretch]; + } } CGImageRelease(image); return self; diff --git a/platform/macos/src/NSImage+MGLAdditions.mm b/platform/macos/src/NSImage+MGLAdditions.mm index 34f0325d6f..2bb4fae602 100644 --- a/platform/macos/src/NSImage+MGLAdditions.mm +++ b/platform/macos/src/NSImage+MGLAdditions.mm @@ -27,11 +27,18 @@ - (nullable instancetype)initWithMGLStyleImage:(const mbgl::style::Image &)style NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:image]; CGImageRelease(image); - CGFloat w = styleImage.getImage().size.width / styleImage.getPixelRatio(); - CGFloat h = styleImage.getImage().size.height / styleImage.getPixelRatio(); - if (self = [self initWithSize:NSMakeSize(w, h)]) { + CGFloat scale = styleImage.getPixelRatio(); + NSSize size = NSMakeSize(styleImage.getImage().size.width / scale, + styleImage.getImage().size.height / scale); + if (self = [self initWithSize:size]) { [self addRepresentation:rep]; [self setTemplate:styleImage.isSdf()]; + if (auto content = styleImage.getContent()) { + self.capInsets = NSEdgeInsetsMake(content->top / scale, + content->left / scale, + size.height - content->bottom / scale, + size.width - content->right / scale); + } } return self; }