地图标注集群

问题描述 投票:0回答:0

我需要在地图上显示一组注释(集群)的照片。照片应从该集群内的注释中随机选择。目前,只有在“AllPins”类中为“pinToDisplay”分配静态变量时,我的方法才有效。但是,当添加大量图钉时,这会导致内存泄漏,因为它存储来自 Firebase 数据的照片。如果我从变量中删除静态属性,内存使用会变得更有效率,但集群无法显示一组注释的照片。它仍然适用于单个注释。

代码在这里

class PinParsed: Codable {
    let createdDate, id, owner, updatedDate: String?
     let address: String?
     let approve: Bool?
     let category: [String]?
     let city: String?
     let country: String?
     let creator: String?
     let image: String?
     let img: String?
     let lat: String?
     let linkPinAll: String?
     let linkPinTitle, long: String?
     let subscrtag: [String]?
     let title: String?
     let gal: [GalClass]?
     let instagramLink, googleStreetViewURL: String?
}

class PinAnnotation : MKPointAnnotation {
    var pin: PinParsed!
    var image: String!
}

final class AllPins {

      static var pinToDisplay: [PinAnnotation]?
    deinit {
        print("allpins deinit")
    }

//This is in the class which is responsible for cluster

override var annotation: MKAnnotation? {
        didSet {
            guard let annotation = annotation as? MKClusterAnnotation else {
                return
            }
            countLabel.text = "\(annotation.memberAnnotations.count)"
        }
        
        
        willSet {
            guard let annotation = annotation as? MKClusterAnnotation else {
                return
            }
            var randomElement = annotation.memberAnnotations.randomElement()
            let latitude = randomElement?.coordinate.latitude
            let longitude = randomElement?.coordinate.longitude
            if latitude != nil, longitude != nil {
                if let allPins = AllPins.pinToDisplay {
                    allPins.forEach { pinAnnotation in
                        
                        if pinAnnotation.coordinate.latitude == latitude && pinAnnotation.coordinate.longitude == longitude {
                            if let url = URL(string: pinAnnotation.pin.img!) {
                                URLSession.shared.dataTask(with: url) { [weak self] (data, response, error) in
                                    if let error = error {
                                        print(error.localizedDescription, "cluster photo")
                                    }
                                    if let imageData = data  {
                                        DispatchQueue.main.async {
                                            self!.imageView.image = UIImage(data: imageData)
                                            self!.imageView.contentMode = .scaleAspectFill
                                        }
                                    }
                                }.resume()
                            }
                        }
                    }
                }
                
            }
        }
ios swift annotations mapkit mkannotation
© www.soinside.com 2019 - 2024. All rights reserved.