From 682ef63d7887e860584105d79ed3569df86f1bf7 Mon Sep 17 00:00:00 2001 From: Wei Chen Date: Thu, 20 Nov 2025 16:21:52 +0800 Subject: [PATCH] support hide attribution on iOS side (#3952) Co-authored-by: Wei Chen Co-authored-by: Bart Louwers --- platform/darwin/src/MLNMapSnapshotter.h | 6 +++- platform/darwin/src/MLNMapSnapshotter.mm | 36 +++++++++++-------- platform/ios/app/MBXSnapshotsViewController.m | 17 ++++----- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/platform/darwin/src/MLNMapSnapshotter.h b/platform/darwin/src/MLNMapSnapshotter.h index ef8352ede..3092bdd83 100644 --- a/platform/darwin/src/MLNMapSnapshotter.h +++ b/platform/darwin/src/MLNMapSnapshotter.h @@ -79,11 +79,15 @@ MLN_EXPORT // MARK: - Configuring the Map /** - :nodoc: Whether to include the MapLibre logo. Note this is not required. */ @property (nonatomic, readwrite) BOOL showsLogo; +/** + Whether to add attribution from the sources on the snapshots. Enabled by default. + */ +@property (nonatomic, readwrite) BOOL showsAttribution; + /** URL of the map style to snapshot. */ diff --git a/platform/darwin/src/MLNMapSnapshotter.mm b/platform/darwin/src/MLNMapSnapshotter.mm index ed474083f..6f632f369 100644 --- a/platform/darwin/src/MLNMapSnapshotter.mm +++ b/platform/darwin/src/MLNMapSnapshotter.mm @@ -146,6 +146,7 @@ private: _size = size; _camera = camera; _showsLogo = YES; + _showsAttribution = YES; #if TARGET_OS_IPHONE _scale = [UIScreen mainScreen].scale; #else @@ -161,6 +162,7 @@ private: copy.coordinateBounds = _coordinateBounds; copy.scale = _scale; copy.showsLogo = _showsLogo; + copy.showsAttribution = _showsAttribution; return copy; } @@ -403,22 +405,24 @@ MLNImage *MLNAttributedSnapshot(mbgl::MapSnapshotter::Attributions attributions, return nil; } - if (options.showsLogo) { + if (options.showsLogo && logoImage) { [logoImage drawInRect:logoImageRect]; } - UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext(); - CGImageRef attributionImageRef = CGImageCreateWithImageInRect([currentImage CGImage], cropRect); - UIImage *attributionImage = [UIImage imageWithCGImage:attributionImageRef]; - CGImageRelease(attributionImageRef); + if (options.showsAttribution) { + UIImage *currentImage = UIGraphicsGetImageFromCurrentImageContext(); + CGImageRef attributionImageRef = CGImageCreateWithImageInRect([currentImage CGImage], cropRect); + UIImage *attributionImage = [UIImage imageWithCGImage:attributionImageRef]; + CGImageRelease(attributionImageRef); - CIImage *ciAttributionImage = [[CIImage alloc] initWithCGImage:attributionImage.CGImage]; + CIImage *ciAttributionImage = [[CIImage alloc] initWithCGImage:attributionImage.CGImage]; - UIImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:ciAttributionImage]; + UIImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:ciAttributionImage]; - [blurredAttributionBackground drawInRect:attributionBackgroundFrame]; + [blurredAttributionBackground drawInRect:attributionBackgroundFrame]; - [MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo]; + [MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo]; + } UIImage *compositedImage = UIGraphicsGetImageFromCurrentImageContext(); @@ -478,19 +482,21 @@ MLNImage *MLNAttributedSnapshot(mbgl::MapSnapshotter::Attributions attributions, return nil; } - if (logoImage) { + if (options.showsLogo) { [logoImage drawInRect:logoImageRect]; } - NSBitmapImageRep *attributionBackground = [[NSBitmapImageRep alloc] initWithFocusedViewRect:attributionBackgroundFrame]; + if (options.showsAttribution) { + NSBitmapImageRep *attributionBackground = [[NSBitmapImageRep alloc] initWithFocusedViewRect:attributionBackgroundFrame]; - CIImage *attributionBackgroundImage = [[CIImage alloc] initWithCGImage:[attributionBackground CGImage]]; + CIImage *attributionBackgroundImage = [[CIImage alloc] initWithCGImage:[attributionBackground CGImage]]; - NSImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:attributionBackgroundImage]; + NSImage *blurredAttributionBackground = [MLNMapSnapshotter blurredAttributionBackground:attributionBackgroundImage]; - [blurredAttributionBackground drawInRect:attributionBackgroundFrame]; + [blurredAttributionBackground drawInRect:attributionBackgroundFrame]; - [MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo]; + [MLNMapSnapshotter drawAttributionTextWithStyle:attributionInfoStyle origin:attributionTextPosition attributionInfo:attributionInfo]; + } [compositedImage unlockFocus]; diff --git a/platform/ios/app/MBXSnapshotsViewController.m b/platform/ios/app/MBXSnapshotsViewController.m index b317bbc83..e88c6dba2 100644 --- a/platform/ios/app/MBXSnapshotsViewController.m +++ b/platform/ios/app/MBXSnapshotsViewController.m @@ -32,22 +32,23 @@ [super viewDidLoad]; // Start snapshotters - topLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTL coordinates:CLLocationCoordinate2DMake(37.7184, -122.4365)]; - topCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTM coordinates:CLLocationCoordinate2DMake(38.8936, -77.0146)]; - topRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTR coordinates:CLLocationCoordinate2DMake(-13.1356, -74.2442)]; + topLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTL coordinates:CLLocationCoordinate2DMake(37.7184, -122.4365) hideAttribution:YES]; + topCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTM coordinates:CLLocationCoordinate2DMake(38.8936, -77.0146) hideAttribution:NO]; + topRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewTR coordinates:CLLocationCoordinate2DMake(-13.1356, -74.2442) hideAttribution:YES]; - bottomLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBL coordinates:CLLocationCoordinate2DMake(52.5072, 13.4247)]; - bottomCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBM coordinates:CLLocationCoordinate2DMake(60.2118, 24.6754)]; - bottomRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBR coordinates:CLLocationCoordinate2DMake(31.2780, 121.4286)]; + bottomLeftSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBL coordinates:CLLocationCoordinate2DMake(52.5072, 13.4247) hideAttribution:NO]; + bottomCenterSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBM coordinates:CLLocationCoordinate2DMake(60.2118, 24.6754) hideAttribution:YES]; + bottomRightSnapshotter = [self startSnapshotterForImageView:_snapshotImageViewBR coordinates:CLLocationCoordinate2DMake(31.2780, 121.4286) hideAttribution:NO]; } -- (MLNMapSnapshotter*) startSnapshotterForImageView:(UIImageView*) imageView coordinates:(CLLocationCoordinate2D) coordinates { +- (MLNMapSnapshotter*) startSnapshotterForImageView:(UIImageView*) imageView coordinates:(CLLocationCoordinate2D) coordinates hideAttribution:(BOOL) hideAttribution { // Create snapshot options MLNMapCamera* mapCamera = [[MLNMapCamera alloc] init]; mapCamera.pitch = 20; mapCamera.centerCoordinate = coordinates; - MLNMapSnapshotOptions* options = [[MLNMapSnapshotOptions alloc] initWithStyleURL:[[MLNStyle predefinedStyle:@"Hybrid"] url] camera:mapCamera size:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)]; + MLNMapSnapshotOptions* options = [[MLNMapSnapshotOptions alloc] initWithStyleURL:[NSURL URLWithString:@"https://tiles.openfreemap.org/styles/liberty"] camera:mapCamera size:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)]; options.zoomLevel = 10; + options.showsAttribution = !hideAttribution; // Create and start the snapshotter __weak UIImageView *weakImageView = imageView;