{"id":732,"date":"2015-01-09T11:00:29","date_gmt":"2015-01-09T02:00:29","guid":{"rendered":"https:\/\/blog.ymyzk.com\/?p=732"},"modified":"2015-01-08T20:53:35","modified_gmt":"2015-01-08T11:53:35","slug":"mkmapsnapshotter","status":"publish","type":"post","link":"https:\/\/blog.ymyzk.com\/2015\/01\/mkmapsnapshotter\/","title":{"rendered":"MKMapSnapshotter \u3092\u4f7f\u3063\u305f\u5730\u56f3\u753b\u50cf\u306e\u751f\u6210"},"content":{"rendered":"

iOS 7 \u4ee5\u964d\u3068 OS X 10.9 \u4ee5\u964d\u3067\u306f MKMapSnapshotter \u3092\u5229\u7528\u3057\u3066, \u5730\u56f3\u306e\u753b\u50cf (UIImage) \u3092\u975e\u540c\u671f\u3067\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u3053\u308c\u3092\u5229\u7528\u3059\u308b\u3068 MKMapView \u3068\u540c\u3058\u898b\u305f\u76ee\u306e\u5730\u56f3\u306e\u753b\u50cf\u3092\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u5185\u3067\u7c21\u5358\u306b\u751f\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d, \u30e6\u30fc\u30b6\u30fc\u306b\u3088\u308b\u64cd\u4f5c\u3092\u53d7\u3051\u3064\u3051\u308b\u5fc5\u8981\u304c\u306a\u3044 MKMapView \u3092 UIImageView \u306a\u3069\u3067\u7f6e\u63db\u3057\u3066\u8ca0\u8377\u3092\u6e1b\u3089\u3057\u305f\u308a, \u5730\u56f3\u753b\u50cf\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n

<\/p>\n

\u30b7\u30f3\u30d7\u30eb\u306a\u4f8b<\/h2>\n

\u3082\u3063\u3068\u3082\u5358\u7d14\u306b\u5730\u56f3\u306e\u753b\u50cf\u3092\u4f5c\u6210\u3059\u308b\u4f8b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n

let center = CLLocationCoordinate2D(latitude: 35.0, longitude: 135.0)\r\nlet span = MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0)\r\nlet options = MKMapSnapshotOptions()\r\noptions.region = MKCoordinateRegion(center: center, span: span)\r\noptions.scale = UIScreen.mainScreen().scale\r\noptions.size = CGSize(width: 320.0, height: 320.0)\r\n\r\nlet snapshotter = MKMapSnapshotter(options: options)\r\n\r\nsnapshotter.startWithCompletionHandler({(snapshot, error) in\r\n    let image = snapshot.image\r\n})\r\n<\/pre>\n

\u307e\u305a, MKMapSnapshotOptions \u3067 region \u3084\u753b\u50cf\u306e\u30b5\u30a4\u30ba\u306a\u3069\u3092\u8a2d\u5b9a\u3057\u307e\u3059. region \u306f MKMapView \u3068\u540c\u3058\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u307e\u3059.<\/p>\n

\u6b21\u306b, MKMapSnapshotter \u3092\u5148\u307b\u3069\u4f5c\u6210\u3057\u305f options \u3092\u4f7f\u3063\u3066\u4f5c\u6210\u3057, startWithCompletionHandler \u3067\u975e\u540c\u671f\u3067\u64ae\u5f71\u51e6\u7406\u3092\u884c\u3044\u307e\u3059.\u3000\u64ae\u5f71\u304c\u7d42\u4e86\u3059\u308b\u3068 completionHandler \u304c\u547c\u3073\u51fa\u3055\u308c\u307e\u3059.\u00a0startWithCompletionHandler \u3092\u5229\u7528\u3059\u308b\u5834\u5408, Swift \u3060\u3068 Closure, Objective-C \u3067\u306f Block \u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059.<\/p>\n

snapshot.image \u306b\u5730\u56f3\u306e\u753b\u50cf\u304c UIImage \u3068\u3057\u3066\u5165\u3063\u3066\u3044\u308b\u306e\u3067, \u3053\u308c\u3092 UIImageView \u3092\u4f7f\u3063\u3066\u8868\u793a\u3057\u305f\u308a, \u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n

MKPinAnnotationView \u306e\u63cf\u753b<\/h2>\n

\u5730\u56f3\u753b\u50cf\u306e\u4e0a\u306b MKPinAnnotationView \u3092\u7528\u3044\u3066\u30d4\u30f3\u3092\u8868\u793a\u3059\u308b\u5834\u5408\u306f, MKMapSnapshotter \u3092\u4f7f\u3063\u3066\u64ae\u5f71\u3057\u305f UIImage \u306e\u4e0a\u306b\u3055\u3089\u306b\u30d4\u30f3\u306e\u753b\u50cf\u3092\u63cf\u753b\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u5b9f\u73fe\u3057\u307e\u3059. MKPinAnnotationView \u3092\u63cf\u753b\u3059\u308b\u4f8b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n

\r\nlet center = CLLocationCoordinate2D(latitude: 35.0, longitude: 135.0)\r\nlet span = MKCoordinateSpan(latitudeDelta: 1.0, longitudeDelta: 1.0)\r\nlet options = MKMapSnapshotOptions()\r\noptions.region = MKCoordinateRegion(center: center, span: span)\r\noptions.scale = UIScreen.mainScreen().scale\r\noptions.size = CGSize(width: 320.0, height: 320.0)\r\n\r\nlet snapshotter = MKMapSnapshotter(options: options)\r\nsnapshotter.startWithQueue(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), completionHandler: {(snapshot, error) in\r\n    if error != nil { return }\r\n    \r\n    let image = snapshot.image\r\n    \r\n    let annotationView = MKPinAnnotationView(annotation: nil, reuseIdentifier: \"Pin\")\r\n    annotationView.pinColor = .Green\r\n    let pinImage = annotationView.image\r\n    \r\n    UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale)\r\n    \r\n    image.drawAtPoint(CGPoint(x: 0.0, y: 0.0))\r\n    \r\n    var point = snapshot.pointForCoordinate(center)\r\n    let pinCenterOffset = annotationView.centerOffset\r\n    point.x -= annotationView.bounds.size.width \/ 2.0\r\n    point.y -= annotationView.bounds.size.height \/ 2.0\r\n    point.x += pinCenterOffset.x\r\n    point.y += pinCenterOffset.y\r\n    pinImage.drawAtPoint(point)\r\n    \r\n    let finalImage = UIGraphicsGetImageFromCurrentImageContext()\r\n    UIGraphicsEndImageContext()\r\n})\r\n<\/pre>\n

options \u306e\u8a2d\u5b9a\u306f\u524d\u306e\u3082\u306e\u3068\u540c\u3058\u3067\u3059. startWithQueue \u3092\u4f7f\u3063\u3066 completionHandler \u3092\u5b9f\u884c\u3059\u308b dispatch queue \u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\u3057\u3066\u3044\u307e\u3059. startWithCompletionHandler \u3092\u7528\u3044\u305f\u5834\u5408\u306f\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u3067 completionHandler \u304c\u5b9f\u884c\u3055\u308c\u307e\u3059.<\/p>\n

completionHandler \u5185\u90e8\u3067 MKPinAnnotationView \u3092\u751f\u6210\u3057\u3066, \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u30d3\u30e5\u30fc\u306e UIImage \u3092\u5730\u56f3\u306e UIImage \u306b\u91cd\u306d\u3066\u63cf\u753b\u3057, \u6700\u7d42\u7684\u306a\u7d50\u679c\u3092 finalImage \u306b\u4ee3\u5165\u3057\u3066\u3044\u307e\u3059. \u3053\u306e\u65b9\u6cd5\u306f\u4ed6\u306e AnnotationView \u3067\u3082\u5229\u7528\u3067\u304d\u308b\u306f\u305a\u3067\u3059.<\/p>\n

\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u306e\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3092\u884c\u3046\u3053\u3068\u306b\u306a\u308b\u306e\u3067, completionHandler \u304b\u3089 UI \u3092\u64cd\u4f5c\u3059\u308b\u5834\u5408\u306a\u3069\u306f\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059. \u5fc5\u8981\u306b\u5fdc\u3058\u3066 dispatch_async(dispatch_get_main_queue(), {})<\/code> \u306a\u3069\u3092\u5229\u7528\u3057\u3066\u304f\u3060\u3055\u3044.<\/p>\n

\u53c2\u8003<\/h2>\n