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

[ios] Add removeStyleImage to MGLMapView #14769

Merged
merged 3 commits into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT

* Removed previously deprecated methods and properties that had been marked `unavailable`. ([#15000](https://github.com/mapbox/mapbox-gl-native/pull/15000))

### Styles and rendering

* Added the `-[MGLMapViewDelegate mapView:shouldRemoveStyleImage:]` method for optimizing style image caching. ([#14769](https://github.com/mapbox/mapbox-gl-native/pull/14769))

### Other changes

* Added variants of several animated `MGLMapView` methods that accept completion handlers ([#14381](https://github.com/mapbox/mapbox-gl-native/pull/14381)):
Expand Down
1 change: 1 addition & 0 deletions platform/ios/src/MGLMapView+Impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class MGLMapViewImpl : public mbgl::MapObserver {
void onSourceChanged(mbgl::style::Source& source) override;
void onDidBecomeIdle() override;
void onStyleImageMissing(const std::string& imageIdentifier) override;
bool onCanRemoveUnusedStyleImage(const std::string& imageIdentifier) override;

protected:
/// Cocoa map view that this adapter bridges to.
Expand Down
5 changes: 5 additions & 0 deletions platform/ios/src/MGLMapView+Impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@
NSString *imageName = [NSString stringWithUTF8String:imageIdentifier.c_str()];
[mapView didFailToLoadImage:imageName];
}

bool MGLMapViewImpl::onCanRemoveUnusedStyleImage(const std::string &imageIdentifier) {
NSString *imageName = [NSString stringWithUTF8String:imageIdentifier.c_str()];
return [mapView shouldRemoveStyleImage:imageName];
}
8 changes: 8 additions & 0 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6234,6 +6234,14 @@ - (void)didFailToLoadImage:(NSString *)imageName {
}
}

- (BOOL)shouldRemoveStyleImage:(NSString *)imageName {
if ([self.delegate respondsToSelector:@selector(mapView:shouldRemoveStyleImage:)]) {
return [self.delegate mapView:self shouldRemoveStyleImage:imageName];
}

return YES;
}

- (void)updateUserLocationAnnotationView
{
[self updateUserLocationAnnotationViewAnimatedWithDuration:0];
Expand Down
14 changes: 14 additions & 0 deletions platform/ios/src/MGLMapViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable UIImage *)mapView:(MGLMapView *)mapView didFailToLoadImage:(NSString *)imageName;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method appears under the “Managing Callout Views” heading in the header and in documentation:

#pragma mark Managing Callout Views

Create a new heading for “Managing the Style” or move this method up to the “Loading the Map” section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On iOS is under the right header.

Asks the delegate whether the map view should evict cached images.

This method is called in two scenarios: when the cumulative size of unused images
exceeds the cache size or when the last tile that includes the image is removed from
memory.

@param mapView The map view that is evicting the image.
@param imageName The image name that is going to be removed.
@return A Boolean value indicating whether the map view should evict
the cached image.
*/
- (BOOL)mapView:(MGLMapView *)mapView shouldRemoveStyleImage:(NSString *)imageName;

#pragma mark Tracking User Location

/**
Expand Down
1 change: 1 addition & 0 deletions platform/ios/src/MGLMapView_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ FOUNDATION_EXTERN MGL_EXPORT MGLExceptionName const _Nonnull MGLUnderlyingMapUna
- (void)mapViewDidFinishLoadingStyle;
- (void)sourceDidChange:(nonnull MGLSource *)source;
- (void)didFailToLoadImage:(nonnull NSString *)imageName;
- (BOOL)shouldRemoveStyleImage:(nonnull NSString *)imageName;

/** Triggers another render pass even when it is not necessary. */
- (void)setNeedsRerender;
Expand Down
2 changes: 2 additions & 0 deletions platform/ios/test/MGLMapViewDelegateIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ extension MGLMapViewDelegateIntegrationTests: MGLMapViewDelegate {
func mapViewUserLocationAnchorPoint(_ mapView: MGLMapView) -> CGPoint { return CGPoint(x: 100, y: 100) }

func mapView(_ mapView: MGLMapView, didFailToLoadImage imageName: String) -> UIImage? { return nil }

func mapView(_ mapView: MGLMapView, shouldRemoveStyleImage imageName: String) -> Bool { return false }
}
1 change: 1 addition & 0 deletions platform/macos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
### Styles and rendering

* Setting `MGLMapView.contentInset` now moves the map’s focal point to the center of the content frame after insetting. ([#14664](https://github.com/mapbox/mapbox-gl-native/pull/14664))
* Added the `-[MGLMapViewDelegate mapView:shouldRemoveStyleImage:]` method for optimizing style image caching. ([#14769](https://github.com/mapbox/mapbox-gl-native/pull/14769))

### Other changes

Expand Down
1 change: 1 addition & 0 deletions platform/macos/src/MGLMapView+Impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class MGLMapViewImpl : public mbgl::MapObserver {
void onDidFinishLoadingStyle() override;
void onSourceChanged(mbgl::style::Source& source) override;
void onDidBecomeIdle() override;
bool onCanRemoveUnusedStyleImage(const std::string& imageIdentifier) override;

protected:
/// Cocoa map view that this adapter bridges to.
Expand Down
5 changes: 5 additions & 0 deletions platform/macos/src/MGLMapView+Impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,8 @@
MGLSource * nativeSource = [mapView.style sourceWithIdentifier:identifier];
[mapView sourceDidChange:nativeSource];
}

bool MGLMapViewImpl::onCanRemoveUnusedStyleImage(const std::string &imageIdentifier) {
NSString *imageName = [NSString stringWithUTF8String:imageIdentifier.c_str()];
return [mapView shouldRemoveStyleImage:imageName];
}
8 changes: 8 additions & 0 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,14 @@ - (void)sourceDidChange:(MGLSource *)source {
self.needsDisplay = YES;
}

- (BOOL)shouldRemoveStyleImage:(NSString *)imageName {
if ([self.delegate respondsToSelector:@selector(mapView:shouldRemoveStyleImage:)]) {
return [self.delegate mapView:self shouldRemoveStyleImage:imageName];
}

return YES;
}

#pragma mark Printing

- (void)print:(__unused id)sender {
Expand Down
14 changes: 14 additions & 0 deletions platform/macos/src/MGLMapViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,20 @@ NS_ASSUME_NONNULL_BEGIN

- (nullable NSImage *)mapView:(MGLMapView *)mapView didFailToLoadImage:(NSString *)imageName;

/**
Asks the delegate whether the map view should evict cached images.

This method is called in two scenarios: when the cumulative size of unused images
exceeds the cache size or when the last tile that includes the image is removed from
memory.

@param mapView The map view that is evicting the image.
@param imageName The image name that is going to be removed.
@return A Boolean value indicating whether the map view should evict
the cached image.
*/
- (BOOL)mapView:(MGLMapView *)mapView shouldRemoveStyleImage:(NSString *)imageName;

#pragma mark Managing the Appearance of Annotations

/**
Expand Down
1 change: 1 addition & 0 deletions platform/macos/src/MGLMapView_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace mbgl {
- (void)mapViewDidBecomeIdle;
- (void)mapViewDidFinishLoadingStyle;
- (void)sourceDidChange:(nonnull MGLSource *)source;
- (BOOL)shouldRemoveStyleImage:(nonnull NSString *)imageName;

/// Asynchronously render a frame of the map.
- (void)setNeedsRerender;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ extension MGLMapViewDelegateIntegrationTests: MGLMapViewDelegate {

func mapView(_ mapView: MGLMapView, calloutViewControllerFor annotation: MGLAnnotation) -> NSViewController? { return nil }

func mapView(_ mapView: MGLMapView, shouldRemoveStyleImage imageName: String) -> Bool { return false }
}