Skip to content
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

Add snappshotter options for logo and attribution #553

Merged
merged 8 commits into from
Aug 4, 2021
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Mapbox welcomes participation and contributions from everyone.

* Fixed an issue where `MapView` positioning wasn't correct when used in containers such as UIStackView. ([#533](https://github.com/mapbox/mapbox-maps-ios/pull/533))

### Features ✨ and improvements 🏁
* Added new options to `MapSnapshotOptions`
* `showsLogo` is a flag that will decide whether the logo will be shown on a snapshot
* `showsAttribution` is a flag that will decide whether the attribution will be shown on a snapshot

## 10.0.0-rc.4 - July 14, 2021

### Features ✨ and improvements 🏁
Expand Down
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ let package = Package(
.copy("Foundation/GeoJSON/Fixtures/multipolygon.geojson"),
.copy("Helpers/MapboxAccessToken"),
.copy("Resources/empty-style-chicago.json"),
.copy("Resources/testShowsLogo().png"),
.copy("Resources/testDoNotShowsLogo().png"),
.copy("Resources/testShowsAttribution().png"),
.copy("Resources/testDoNotShowsAttribution().png"),
.process("Snapshot/Snapshot.xcassets"),
.process("Resources/MapInitOptionsTests.xib"),
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/// Set of options for taking map snapshot with Snapshotter.
public struct MapSnapshotOptions {
/// Dimensions of the snapshot in points.
public let size: CGSize
public var size: CGSize

/// Ratio between the number device-independent and screen pixels.
public let pixelRatio: CGFloat
public var pixelRatio: CGFloat

/// Glyphs rasterization options to use for client-side text rendering.
/// By default, `GlyphsRasterizationOptions` will use `.ideographsRasterizedLocally`
public let glyphsRasterizationOptions: GlyphsRasterizationOptions
public var glyphsRasterizationOptions: GlyphsRasterizationOptions

/// Resource fetching options to be used by the snapshotter.
public let resourceOptions: ResourceOptions
public var resourceOptions: ResourceOptions

/// Flag that determines if the logo should be shown on the snapshot
public var showsLogo: Bool

/// Flag that determines if attribution should be shown on the snapshot
public var showsAttribution: Bool

/// Initializes a `MapSnapshotOptions`
/// - Parameters:
Expand All @@ -23,10 +29,14 @@ public struct MapSnapshotOptions {
/// - resourceOptions: Resource fetching options to be used by the
/// snapshotter. Default uses the access token provided by
/// `ResourceOptionsManager.default`
/// - showsLogo: Flag that determines if the logo should be shown on the snapshot
/// - showsAttribution: Flag that determines if attribution should be shown on the snapshot
public init(size: CGSize,
pixelRatio: CGFloat,
glyphsRasterizationOptions: GlyphsRasterizationOptions = GlyphsRasterizationOptions(),
resourceOptions: ResourceOptions = ResourceOptionsManager.default.resourceOptions) {
resourceOptions: ResourceOptions = ResourceOptionsManager.default.resourceOptions,
showsLogo: Bool = true,
showsAttribution: Bool = true) {
precondition(pixelRatio > 0)
precondition(size.width * pixelRatio <= 8192, "Width or scale too great.")
precondition(size.height * pixelRatio <= 8192, "Height or scale too great.")
Expand All @@ -35,6 +45,8 @@ public struct MapSnapshotOptions {
self.pixelRatio = pixelRatio
self.glyphsRasterizationOptions = glyphsRasterizationOptions
self.resourceOptions = resourceOptions
self.showsLogo = showsLogo
self.showsAttribution = showsAttribution
}
}

Expand Down
9 changes: 7 additions & 2 deletions Sources/MapboxMaps/Snapshot/Snapshotter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import UIKit
import CoreLocation
import CoreImage.CIFilterBuiltins

@_implementationOnly import MapboxCommon_Private

// MARK: - Snapshotter
public class Snapshotter {

Expand Down Expand Up @@ -80,6 +82,8 @@ public class Snapshotter {
let scale = CGFloat(options.pixelRatio)

let style = self.style
let options = self.options

mapSnapshotter.start { (expected) in
if expected.isError() {
completion(.failure(.snapshotFailed(reason: expected.error as? String)))
Expand Down Expand Up @@ -180,11 +184,12 @@ public class Snapshotter {
context.restoreGState()
}

if true { // options.showsLogo
if options.showsLogo {
Snapshotter.renderLogoView(logoView, context: context)
}

if let attributionView = attributionView, true { // options.showsAttribution
if let attributionView = attributionView,
options.showsAttribution {
Snapshotter.renderAttributionView(attributionView,
blurredImage: blurredImage,
context: context)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions Tests/MapboxMapsTests/Snapshot/MapboxMapsSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,63 @@ class MapboxMapsSnapshotTests: XCTestCase {
}
}

func testShowsLogo() throws {
var options = try snapshotterOptions()
options.showsLogo = true

showLogoAttributionHelper(options: options, fileName: "\(#function)")
}

func testDoNotShowsLogo() throws {
var options = try snapshotterOptions()
options.showsLogo = false

showLogoAttributionHelper(options: options, fileName: "\(#function)")
}

func testShowsAttribution() throws {
var options = try snapshotterOptions()
options.showsAttribution = true

showLogoAttributionHelper(options: options, fileName: "\(#function)")
}

func testDoNotShowsAttribution() throws {
var options = try snapshotterOptions()
options.showsAttribution = false

showLogoAttributionHelper(options: options, fileName: "\(#function)")
}

private func showLogoAttributionHelper(options: MapSnapshotOptions, fileName: String) {
let snapshotter = Snapshotter(options: options)

// Adding a simple custom style
snapshotter.style.JSON = #"{"version":8,"sources":{},"layers":[{"id":"background","type":"background","paint":{"background-color":"white"}}]}"#

let expectation = self.expectation(description: "snapshot")
snapshotter.start(overlayHandler: nil, completion: { result in
switch result {
case let .success(image) :
// This code block is used to generate an image for comparison.
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("\(fileName).png")
do {
try image.pngData()?.write(to: url)
} catch {
print(error)
}

// Compare snapshot asset data vs snapshot image data
let path = Bundle.mapboxMapsTests.path(forResource: "\(fileName)", ofType: "png")!
let compareUrl = URL(fileURLWithPath: path)
let expectedImageData = try! Data(contentsOf: compareUrl)

XCTAssertEqual(expectedImageData, image.pngData())
case.failure :
XCTFail("Failure: snapshot asset and snapshot image do not match")
}
expectation.fulfill()
})
wait(for: [expectation], timeout: 10)
}
}