Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
[ios, macos] Roundtrip resizable image cap insets
Browse files Browse the repository at this point in the history
  • Loading branch information
1ec5 committed Mar 10, 2020
1 parent cbb21ab commit 4e0a77e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
18 changes: 18 additions & 0 deletions platform/darwin/test/MGLImageTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion platform/ios/src/UIImage+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 10 additions & 3 deletions platform/macos/src/NSImage+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 4e0a77e

Please sign in to comment.