就将其“保存”在内存中而言,您可以NSData
通过 Objective-C 完成块(或 Swift 闭包)返回。从那里,您可以将 传递NSData
给另一个方法或将其保存在类属性中。
例如,在 Objective-C 中:
/** Request NSData of PNG representation of map snapshot.
*
* @param mapView The MKMapView for which we're capturing the snapshot
* @param completion The completion block that will be called when the asynchronous snapshot is done. This takes two parameters, the resulting NSData upon success and an NSError if there was an error.
*/
- (void)requestSnapshotDataForMapView:(MKMapView *)mapView completion:(void (^)(NSData *data, NSError *error))completion
{
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = mapView.region;
options.size = mapView.frame.size;
options.scale = [[UIScreen mainScreen] scale];
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (error) {
if (completion) {
completion(nil, error);
}
return;
}
UIImage *image = snapshot.image;
NSData *data = UIImagePNGRepresentation(image);
if (completion) {
completion(data, nil);
}
}];
}
- (IBAction)didTouchUpInsideButton:(id)sender
{
[self requestSnapshotDataForMapView:self.mapView completion:^(NSData *data, NSError *error) {
if (error) {
NSLog(@"requestSnapshotDataForMapView error: %@", error);
return;
}
// do whatever you want with the `data` parameter here, for example,
// if you had some `@property (nonatomic, strong) NSData *snapshotData;`,
// you might do:
//
// self.snapshotData = data
// now initiate the next step of the process in which you're presumably
// going to use the `data` provided by this block.
}];
}
Swift 等价物是:
/// Request NSData of PNG representation of map snapshot.
///
/// - parameter mapView: The MKMapView for which we're capturing the snapshot
/// - parameter completion: The closure that will be called when the asynchronous snapshot is done. This takes two parameters, the resulting NSData upon success and an NSError if there was an error.
func requestSnapshotData(mapView: MKMapView, completion: (NSData?, NSError?) -> ()) {
let options = MKMapSnapshotOptions()
options.region = mapView.region
options.size = mapView.frame.size
options.scale = UIScreen.mainScreen().scale
let snapshotter = MKMapSnapshotter(options: options)
snapshotter.startWithCompletionHandler() { snapshot, error in
guard snapshot != nil else {
completion(nil, error)
return
}
let image = snapshot!.image
let data = UIImagePNGRepresentation(image)
completion(data, nil)
}
}
@IBAction func didTouchUpInsideButton(sender: AnyObject) {
requestSnapshotData(mapView) { data, error in
guard data != nil else {
print("requestSnapshotData error: \(error)")
return
}
// do whatever you want with the `data` parameter here, for example,
// if you had some `var snapshotData: NSData?` class property, you might do:
//
// self.snapshotData = data
// now initiate the next step of the process in which you're presumably
// going to use the `data` provided by this closure.
}
}
坦率地说,如果你想在UIImage
其他地方使用,我可能会更改这些块/闭包参数以使用UIImage
而不是NSData
,但希望这能说明这个想法。