[ios, macos] Roundtrip resizable image cap insets

This commit is contained in:
Minh Nguyễn
2020-03-05 14:17:15 -08:00
committed by Minh Nguyễn
parent c225a50f25
commit a18d5211a6
3 changed files with 39 additions and 4 deletions

View File

@@ -40,6 +40,12 @@
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 @@
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 @@
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);
}
}
}

View File

@@ -18,12 +18,22 @@ BOOL MGLEdgeInsetsIsZero(UIEdgeInsets edgeInsets) {
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;

View File

@@ -27,11 +27,18 @@ BOOL MGLEdgeInsetsIsZero(NSEdgeInsets edgeInsets) {
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;
}