5

मेरे आईओएस swift ऐप में मेरे पास UITableViewController है जो गतिशील रूप से जोड़े गए कोशिकाओं के साथ है। प्रत्येक सेल में MKMapView एम्बेडेड होता है और मैं अलग-अलग निर्देशांक पर प्रत्येक सेल के लिए मानचित्र का केंद्र सेट कर रहा हूं।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) 
} 

अंदर cellForRowAtIndexPath:: मैं इस विधि को फोन करके इसलिए कर रहा हूँ

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 का उपयोग करता है यह दृष्टिकोण संसाधनों पर कोमल है।

यहां कुछ कोड है जिसे मैंने आपकी स्थिति में अनुकूलित किया है। मैंने विशेष रूप से कैश करने के तरीके के बारे में विस्तार शामिल नहीं किया है, क्योंकि यह इस बात पर निर्भर करता है कि आप अपने ऐप में कैशिंग को कैसे संभालना चाहते हैं। मैंने हनेके कोकोपॉड का उपयोग किया है: अतीत में 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 
        } 
       }) 
संबंधित मुद्दे

 संबंधित मुद्दे