Rya*_*yan 3 objective-c mkmapview ios mkmapsnapshotter swift
我正试图拍摄我的MKMapView的截图.我通常使用Objective C中的以下代码实现此目的.
我想知道如何将NSData对象保存在内存中,而不是保存图像,然后立即从中读取.
我也想知道如何在Swift中编写它 - 特别是完成处理程序部分.我查看了文档 - 但不确定语法:https://developer.apple.com/library/prerelease/iOS/documentation/MapKit/Reference/MKMapSnapshotter_class/index.html#//apple_ref/c/tdef/MKMapSnapshotCompletionHandler
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = self.mapView.region;
options.size = self.mapView.frame.size;
options.scale = [[UIScreen mainScreen] scale];
NSURL *fileURL = [NSURL fileURLWithPath:@"path/to/snapshot.png"];
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (error) {
NSLog(@"[Error] %@", error);
return;
}
UIImage *image = snapshot.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToURL:fileURL atomically:YES];
}];
Run Code Online (Sandbox Code Playgroud)
就"将其保留"在内存中而言,您可以返回NSDatavia-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.
}];
}
Run Code Online (Sandbox Code Playgroud)
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.
}
}
Run Code Online (Sandbox Code Playgroud)
坦率地说,如果你试图使用UIImage其他地方,我可能会改变这些块/闭包参数来使用UIImage而不是NSData,但希望这说明了这个想法.
| 归档时间: |
|
| 查看次数: |
2280 次 |
| 最近记录: |