Open Rheaparks opened 2 years ago
Any news on this topic?
I Have same issue on UIKit project,start
never produces result in completion.
Also seeing this
Same here with SwiftUI. Can someone from Mapbox do a check ?
respectfully the v6->v10 migration is the most frustrating migration I have ever done. half the code is undocumented and its really annoying when stuff like this doesn't work
here's the fix:
// if you dont set this, the snapshotter result closure is never called, lmao!
snapshotter.style.uri = mapView.mapboxMap.style.uri
dont forget to do this or it takes a default screenshot of Brooklyn.
let cameraOptions = CameraOptions(cameraState: self.mapView.cameraState)
snapshotter.setCamera(to: cameraOptions)
my tips for people doing the migration
Thank you @tom-skydio! Your answer is coming just as I'm going to seriously need the feature! I'll be sure to try your tips ☺️
@tom-skydio you are lucky. On my side, even with style URI and camera set-up, the completion block is never called ... Are you using it with SwiftUI ?
fwiw @Juju31Fr im using 10.9.0-rc.1
and no im using it within UIKit
Ok. I’m using 10.8.1 with SwiftUI. @RheaParks you will probably be able to confirm as you are using SwiftUI as well.
OK This works now. Everything has to be done in the main thread.
let snapshotter: Snapshotter = Snapshotter(options: options)
snapshotter.style.uri = mapView.mapboxMap.style.uri
let cameraOptions = CameraOptions(cameraState: mapView.cameraState)
snapshotter.setCamera(to: cameraOptions)
DispatchQueue.main.async {
print("Snapshotting...")
snapshotter.start(overlayHandler: nil, completion: { result in
print("Result is \(result)")
switch result {
case .success(let image):
let snapshotImage = image
print("Snapshot yay")
case .failure(let error):
print("Snapshot generation failed with error: \(error)")
}
})
}
help me, mine's still not working
@haikallf incase you're still struggling with this issue. In Swift, local variables are deallocated once the scope in which they are defined is exited, if there are no strong references to them. This could mean that your snapshotter is being deallocated before it can complete the snapshot.
You should keep a strong reference to the snapshotter for the duration of its operation. You can do this by defining a property in your class class to hold the snapshotter. For example:
var snapshotter: Snapshotter?
private func snapshotMap() {
self.snapshotter = Snapshotter(options: options)
self.snapshotter?.style.uri = mapView.mapboxMap.style.uri
let cameraOptions = CameraOptions(cameraState: mapView.cameraState)
self.snapshotter?.setCamera(to: cameraOptions)
DispatchQueue.main.async {
print("Snapshotting...")
self.snapshotter?.start(overlayHandler: nil) { [weak self] (result) in
print("Result is \(result)")
switch result {
case .success(let image):
let snapshotImage = image
print("Snapshot yay")
case .failure(let error):
print("Snapshot generation failed with error: \(error)")
}
}
}
}
Try that out and see if that helps :)
Environment
Observed behavior and steps to reproduce
When using the Snapshotter, neither one of its completion blocks are called. Here's some sample code:
Here, any completion blocks inside
makeSnapshot(size:)
are never called. There are no errors printed or returned, it just fails silently.Expected behavior
I'd expect at least an error message to be printed, and more importantly, I would expect the completion blocks to be returned with an error, or even succeed.
Notes / preliminary analysis
This sample code uses SwiftUI. I didn't try the Snapshotter in a UIKit project / component. Maybe it works better there, I don't know.
I tried to put some breakpoints inside the
start(overlayHandler:completion:)
method inSnapshotter
. The code here is executed, but things start to go awry whenMapSnapshotter.start
is called. It's in this completion block that nothing ever gets executed.