Alex K. Alex K. - 5 months ago 178
Swift Question

MKMapSnapshotter takes long time for creating Image

I have a working MKMapSnapshotter in my awakeWithContext and i want to set its image for my imageView.

The problem is, that the MKMapSnapshotter is to slow and the image won't be set. Only one or two seconds later, it creates the snapshot image.

override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)

var finalImage = UIImage()
var snapshotterEnd = MKMapSnapshotter()
snapshotterEnd.startWithCompletionHandler(){snapshot, error in

finalImage = snapshot.image
}
imageView.setImage(finalImage)
}


How can i fix this?

Answer

You need to make sure that the completion handler itself will set the image.

snapshotterEnd.startWithCompletionHandler() { snapshot, error in
    imageView.setImage(snapshot.image)
}

The completion block is documented as running on the main thread so you don't need to use dispatch_async to run it there.

Comments