This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 122
Nine-part resizable images #182
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6020cf4
[ios, macos] Nine-part resizable images
1ec5 cbb21ab
[ios, macos] Added image stretching tests
1ec5 4e0a77e
[ios, macos] Roundtrip resizable image cap insets
1ec5 c06937e
[ios, macos] Only set stretchable regions for stretch resizing mode
1ec5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#import <XCTest/XCTest.h> | ||
#import <Mapbox/Mapbox.h> | ||
|
||
#if TARGET_OS_IPHONE | ||
#import "UIImage+MGLAdditions.h" | ||
#define MGLImageResizingModeTile UIImageResizingModeTile | ||
#define MGLImageResizingModeStretch UIImageResizingModeStretch | ||
#else | ||
#import "NSImage+MGLAdditions.h" | ||
#define MGLImageResizingModeTile NSImageResizingModeTile | ||
#define MGLImageResizingModeStretch NSImageResizingModeStretch | ||
#endif | ||
|
||
@interface MGLImageTests : XCTestCase | ||
|
||
@end | ||
|
||
@implementation MGLImageTests | ||
|
||
- (void)testStretching { | ||
#if TARGET_OS_IPHONE | ||
CGRect rect = CGRectMake(0, 0, 24, 24); | ||
UIGraphicsBeginImageContextWithOptions(rect.size, NO, UIScreen.mainScreen.scale); | ||
CGContextRef context = UIGraphicsGetCurrentContext(); | ||
CGContextSetStrokeColorWithColor(context, UIColor.blackColor.CGColor); | ||
CGContextStrokeRectWithWidth(context, rect, 2); | ||
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | ||
UIGraphicsEndImageContext(); | ||
#else | ||
NSImage *image = [NSImage imageWithSize:NSMakeSize(24, 24) flipped:NO drawingHandler:^BOOL(NSRect dstRect) { | ||
// A little more fanciful than the iOS version, but we aren’t testing the actual contents of the image anyways. | ||
NSRectEdge allSides[] = {NSMinYEdge, NSMaxXEdge, NSMaxYEdge, NSMinXEdge, NSMinYEdge, NSMaxXEdge}; | ||
CGFloat grays[] = {NSBlack, NSBlack, NSWhite, NSWhite, NSDarkGray, NSDarkGray}; | ||
dstRect = NSDrawTiledRects(dstRect, dstRect, allSides, grays, sizeof(grays) / sizeof(grays[0])); | ||
[NSColor.grayColor set]; | ||
NSRectFill(dstRect); | ||
return YES; | ||
}]; | ||
image.resizingMode = NSImageResizingModeTile; | ||
#endif | ||
|
||
{ | ||
auto styleImage = [image mgl_styleImageWithIdentifier:@"box"]; | ||
XCTAssert(styleImage); | ||
if (styleImage) { | ||
XCTAssert(!styleImage->getContent()); | ||
XCTAssertFalse(styleImage->isSdf()); | ||
XCTAssertTrue(styleImage->getStretchX().empty()); | ||
XCTAssertTrue(styleImage->getStretchY().empty()); | ||
|
||
MGLImage *imageAfter = [[MGLImage alloc] initWithMGLStyleImage:*styleImage]; | ||
XCTAssertEqual(imageAfter.resizingMode, MGLImageResizingModeTile); | ||
XCTAssertEqual(imageAfter.capInsets.top, 0); | ||
XCTAssertEqual(imageAfter.capInsets.left, 0); | ||
XCTAssertEqual(imageAfter.capInsets.bottom, 0); | ||
XCTAssertEqual(imageAfter.capInsets.right, 0); | ||
} | ||
} | ||
|
||
#if TARGET_OS_IPHONE | ||
image = [image resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch]; | ||
#else | ||
image.resizingMode = NSImageResizingModeStretch; | ||
image.capInsets = NSEdgeInsetsZero; | ||
#endif | ||
{ | ||
auto styleImage = [image mgl_styleImageWithIdentifier:@"box"]; | ||
XCTAssert(styleImage); | ||
if (styleImage) { | ||
auto scale = styleImage->getPixelRatio(); | ||
XCTAssert(!styleImage->getContent()); | ||
|
||
auto stretchX = styleImage->getStretchX(); | ||
XCTAssertEqual(stretchX.size(), 1UL); | ||
if (!stretchX.empty()) { | ||
XCTAssertEqual(stretchX.front(), mbgl::style::ImageStretch(0, 24 * scale)); | ||
} | ||
auto stretchY = styleImage->getStretchY(); | ||
XCTAssertEqual(stretchY.size(), 1UL); | ||
if (!stretchY.empty()) { | ||
XCTAssertEqual(stretchY.front(), mbgl::style::ImageStretch(0, 24 * scale)); | ||
} | ||
|
||
MGLImage *imageAfter = [[MGLImage alloc] initWithMGLStyleImage:*styleImage]; | ||
XCTAssertEqual(imageAfter.resizingMode, MGLImageResizingModeStretch); | ||
XCTAssertEqual(imageAfter.capInsets.top, 0); | ||
XCTAssertEqual(imageAfter.capInsets.left, 0); | ||
XCTAssertEqual(imageAfter.capInsets.bottom, 0); | ||
XCTAssertEqual(imageAfter.capInsets.right, 0); | ||
} | ||
} | ||
|
||
#if TARGET_OS_IPHONE | ||
image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 2, 3, 4)]; | ||
#else | ||
image.capInsets = NSEdgeInsetsMake(1, 2, 3, 4); | ||
#endif | ||
{ | ||
auto styleImage = [image mgl_styleImageWithIdentifier:@"box"]; | ||
XCTAssert(styleImage); | ||
if (styleImage) { | ||
auto scale = styleImage->getPixelRatio(); | ||
auto content = styleImage->getContent(); | ||
XCTAssert(content); | ||
if (content) { | ||
XCTAssertEqual(content->top, 1 * scale); | ||
XCTAssertEqual(content->left, 2 * scale); | ||
XCTAssertEqual(content->bottom, 21 * scale); | ||
XCTAssertEqual(content->right, 20 * scale); | ||
} | ||
|
||
auto stretchX = styleImage->getStretchX(); | ||
XCTAssertEqual(stretchX.size(), 1UL); | ||
if (!stretchX.empty()) { | ||
XCTAssertEqual(stretchX.front(), mbgl::style::ImageStretch(2 * scale, 20 * scale)); | ||
} | ||
auto stretchY = styleImage->getStretchY(); | ||
XCTAssertEqual(stretchY.size(), 1UL); | ||
if (!stretchY.empty()) { | ||
XCTAssertEqual(stretchY.front(), mbgl::style::ImageStretch(1 * scale, 21 * scale)); | ||
} | ||
|
||
MGLImage *imageAfter = [[MGLImage alloc] initWithMGLStyleImage:*styleImage]; | ||
XCTAssertEqual(imageAfter.resizingMode, MGLImageResizingModeStretch); | ||
XCTAssertEqual(imageAfter.capInsets.top, 1); | ||
XCTAssertEqual(imageAfter.capInsets.left, 2); | ||
XCTAssertEqual(imageAfter.capInsets.bottom, 3); | ||
XCTAssertEqual(imageAfter.capInsets.right, 4); | ||
} | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
platform/macos/app/Assets.xcassets/ohio.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "ohio.pdf", | ||
"resizing" : { | ||
"mode" : "9-part", | ||
"center" : { | ||
"mode" : "stretch", | ||
"width" : 15, | ||
"height" : 18 | ||
}, | ||
"cap-insets" : { | ||
"bottom" : 3, | ||
"top" : 3, | ||
"right" : 4, | ||
"left" : 5 | ||
} | ||
} | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused by the
* self.scale
here, compared with the/ self.scale
above. What's going on here? (The macOS version below has*
in both)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-initWithMGLStyleImage:
divides by the scale and-mgl_styleImageWithIdentifier:
multiplies by the scale on both platforms.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see what you mean – you’re referring to the
stretchX
andstretchY
variables. Yes, I think the division above is incorrect. The tests aren’t catching the mistake because they only assert on the image’s content rect, not its stretchable areas per se.