-
Notifications
You must be signed in to change notification settings - Fork 191
Resizable image support #1269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resizable image support #1269
Conversation
Sources/MapboxMaps/Style/Style.swift
Outdated
| id: id, | ||
| stretchX: [ImageStretches(first: stretchXFirst, second: stretchXSecond)], | ||
| stretchY: [ImageStretches(first: stretchYFirst, second: stretchYSecond)], | ||
| content: content) |
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.
This implementation is a bit different here in compared to both Android and the v9 SDK.
In Android 9-patch image can also contain a padding box defining where the overlayed text can be drawn. So the Android SDK provides a derivative of that as the image content - source. iOS doesn't provide the ability to define content padding for a resizable image.
The v9 iOS SDK defines the image content area to be the same as the stretchable area of the image - source. While this produces a good looking result by default, it makes impossible to draw content on a resizable part of the image.
I decided to leave the image content as a parameter accessible, if needed, to the user. To make the API more Swifty the parameter type could be UIEdgeInsets instead of ImageContent, as edge insets should be more familiar to iOS developers.
Another point about the image content is that, for example, SymbolLayer has iconTextFitPadding property. Which does pretty much the same job as image content here. Though there probably might be some cases where image content is the only option to provide a content drawing box.
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.
LGTM! I also like the conscious change to addImageResizeable as it will make it easier for customers to encounter in the autocomplete.
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.
Do we need separate methods for addImage and addImageResizable? What if we just offered addImage and made it respect cap insets by default (and potentially had it take a flag to disable that behavior)?
Good point. Indeed, there's no immediate need to have two separate methods for regular and resizable images. It was a conscious decision. Adding resizable image support to the existing method might be considered a breaking change, as it would lead to changes in behaviour of the method when a resizable image is supplied. I think it's a sound idea to add an opt out flag, however I feel that will make the "addImage(..." method even more crowded. func addImage(_ image: UIImage,
id: String,
sdf: Bool = false,
enableResizing: Bool = true,
stretchX: [ImageStretches],
stretchY: [ImageStretches],
content: ImageContent? = nil) throwsIf I understand your suggestion correctly, one would need to explicitly state that they want image to be resized by setting the flag. In that case I think there's very little difference between that and using a distinct method to add a resizable image. I wonder if we could change the behaviour of |
Sources/MapboxMaps/Style/Style.swift
Outdated
| guard image.capInsets != .zero else { | ||
| Log.warning(forMessage: "The supplied image is missing cap insets and might be resized incorrectly.") | ||
| try addImage(image, id: id, stretchX: [], stretchY: []) | ||
| return | ||
| } |
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.
This guard statement makes me think that we don't need a separate methods.
It looks like we can introduce united addImage method and highlight in the description that this method supports resizable images.
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.
Agreed. This is what I had in mind: #1269 (review)
Every UIImage has a non-optional capInsets property. It just defaults to zero for non-resizable images.
It's an interesting problem on definition a "breaking changes". As for me, we should not consider that kind of changes as a breaking one. I feel like we had a bug which we just fixed. |
|
My suggestion:
and document that it respects UIImage.capInsets but not I'd mentioned possibly adding a flag about whether to respect capInsets — that was targeted towards the new method — but thinking about it more, it seems unnecessary. Developers who don't want resizing behavior should just pass images with |
| /// Setting this to `true` allows template images to be recolored. The | ||
| /// default value is `false`. | ||
| /// - contentInsets: The distances the edges of content are inset from the image rectangle. | ||
| /// If present, and if the icon uses icon-text-fit, the |
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.
Is this specific to symbol layer?
Co-authored-by: Andrew Hershberger <[email protected]>
e4ced50 to
1410ea8
Compare
This PR provides an API to add a resizable image to the style without calculating
stretchXandstretchY.The resizable image can be defined either in Xcode asset catalog or programmatically, e.g.:
Which would produce the following result:
Simulator.Screen.Recording.-.iPhone.13.-.2022-04-13.at.12.15.27.mp4
Public API additions to Style
The method to add a resizable image
A convenience method for adding regular images
Pull request checklist:
Tests are not written as Style is not particularly testable at this point
## mainheading near the top).