5

私のios swiftアプリでは、私はUITableViewControllerのセルを動的に追加しました。各セルにはMKMapViewが埋め込まれており、各セルのマップの中心を別の座標に設定しています。私は、このメソッドを呼び出すことによって、そうやってる:cellForRowAtIndexPath内部UITableViewControllerの各セルには、マップが埋め込まれています。代わりに地図のスクリーンショットに変更する方法はありますか?

func centerMapOnLocation(location: CLLocation, map: MKMapView, radius: CLLocationDistance) { 
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 
     radius * 2.0, radius * 2.0) 
    map.setRegion(coordinateRegion, animated: true) 
} 

override func tableView(tview: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tview.dequeueReusableCellWithIdentifier("cell") as! SingleCall 
    let user:SingleUser = self.items[indexPath.row] as! SingleUser 

    let regionRadius: CLLocationDistance = 150 
    let initialLocation = CLLocation(latitude: user.coordinate.latitude, longitude: user.coordinate.longitude) 

    centerMapOnLocation(initialLocation, map: cell.eventMap, radius: regionRadius) 

    cell.eventMap.zoomEnabled = false 
    cell.eventMap.scrollEnabled = false 
    cell.eventMap.userInteractionEnabled = false 
} 

これは罰金であり、それは動作しますが、私はたくさんのレコードを想像メモリに問題があるでしょう - 今もユーザーがテーブルをスクロールしたときにカップルのセルのみが表示されます。各マップは即座に読み込まれ、計算に要する計算量を想像することができます。

私の質問は、地図の実際の位置を多分スクリーンショットのようなものにダイナミックマップビューを変更する方法はありますか?それは多くの細胞になると速く働くでしょうか?

答えて

2

はい、可能です。これを行うには、UIImageView(以下のコードでmapImageViewと呼ぶ)とMKMapSnapshotterを使用するように切り替える必要があります。物を実際に飛ばすには、MKMapSnapshotterが生成する画像をキャッシュして、ユーザーが前に見たセルにスクロールバックしたときに即座に読み込むようにする必要があります。この手法は、各セルのマップではなく、グローバルMKMapSnapShotterを1つしか使用しないため、リソースには穏やかです。

ここに私があなたの状況に適応したコードがあります。具体的にどのようにキャッシュするかについての詳細は含まれていません。なぜなら、それはあなたのアプリでのキャッシュをどのように処理するかによって決まるからです。私は過去に羽根ココアポッド:https://cocoapods.org/pods/Hanekeを使用しました。また、私は以下の共通のスナップショットオプションの多くを設定しましたが、おそらくあなたのユースケースに合わせるべきです。

//CHECK FOR CACHED MAP, IF NOT THEN GENERATE A NEW MAP SNAPSHOT  
let coord = CLLocationCoordinate2D(latitude: placeLatitude, longitude: placeLongitude) 
       let snapshotterOptions = MKMapSnapshotOptions() 
       snapshotterOptions.scale = UIScreen.mainScreen().scale 
       let squareDimension = cell.bounds.width < cell.bounds.height ? (cell.bounds.width)! : (cell.bounds.height)! 
       snapshotterOptions.size = CGSize(width: squareDimension, height: squareDimension) 
       snapshotterOptions.mapType = MKMapType.Hybrid 
       snapshotterOptions.showsBuildings = true 
       snapshotterOptions.showsPointsOfInterest = true 
       snapshotterOptions.region = MKCoordinateRegionMakeWithDistance(coord, 5000, 5000) 

let snapper = MKMapSnapshotter(options: snapshotterOptions) 

       snapper.startWithCompletionHandler({ (snap, error) in 
        if(error != nil) { 
         print("error: " + error!.localizedDescription) 
         return 
        } 
        dispatch_async(dispatch_get_main_queue()) { 
         guard let snap = snap where error == nil else { return } 

         if let indexPaths = self.tview.indexPathsForVisibleRows { 
          if indexPaths.contains(indexPath) { 
           cell.mapImageView.image = snap 
          } 
         } 
         //SET CACHE HERE 
        } 
       }) 
関連する問題