我很难下载图片。我正在使用 Firebase 中的图像 URL 下载 jpeg,并在 map View 标注中使用该图像。目前,标注以正确的大小显示,但没有图像或字幕。当我手动传入图像 Assets 时,它可以完美运行。图像 URL 完美地保存到 newDog 对象。当我调试和检查图像时,它出现在内存中但看起来是空的。
我认为这可能与在图像下载完成之前加载标注 View 有关?
这是我的 viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.map.mapType = .standard
self.map.showsUserLocation = true
self.map.userTrackingMode = .follow
self.map.delegate = self
self.map.mapType = .hybrid
let userDogRef = FIRDatabase.database().reference().child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("dogs")
userDogRef.observe(.childAdded, with: { (snapshot) in
if snapshot.value == nil {
print("no new dog found")
} else {
print("new dog found")
let snapshotValue = snapshot.value as! Dictionary<String, String>
let dogID = snapshotValue["dogID"]!
let dogRef = FIRDatabase.database().reference().child("dogs").child(dogID)
dogRef.observeSingleEvent(of: .value, with: { (snap) in
print("Found dog data!")
let value = snap.value as! Dictionary<String, String>
let name = value["name"]!
let breed = value["breed"]!
let creator = value["creator"]!
let score = Int(value["score"]!)!
let lat = Double(value["latitude"]!)!
let lon = Double(value["longitude"]!)!
let url = value["imageURL"]!
let location = CLLocationCoordinate2D(latitude: lat, longitude: lon)
let newDog = Dog()
newDog.name = name
newDog.breed = breed
newDog.creator = creator
newDog.score = score
newDog.imageURL = url
newDog.location = location
let downloadURL = URL(string: newDog.imageURL)!
URLSession.shared.dataTask(with: downloadURL, completionHandler: { (data, response, error) in
if error != nil {
print(error!)
return
}
newDog.picture = UIImage(data: data!)!
self.dogs.append(newDog)
let annotation = CustomAnnotation(location: newDog.location, title: newDog.name, subtitle: newDog.creator)
DispatchQueue.main.async {
self.map.addAnnotation(annotation)
}
}).resume()
})
}
})
}
这是我的 map View 和注释方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if(annotation is MKUserLocation){
return nil
}
let ident = "pin"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: ident)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: ident)
annotationView?.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
configureDetailView(annotationView!)
return annotationView
}
func configureDetailView(_ annotationView: MKAnnotationView) {
let width = 300
let height = 300
let dogPhotoView = UIView()
let views = ["dogPhotoView": dogPhotoView]
dogPhotoView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[dogPhotoView(\(height))]", options: [], metrics: nil, views: views))
dogPhotoView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[dogPhotoView(\(width))]", options: [], metrics: nil, views: views))
for dog in self.dogs {
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageView.image = dog.picture
}
annotationView.detailCalloutAccessoryView = dogPhotoView
}
我正在使用自定义注释类。就是这样:
class CustomAnnotation: NSObject, MKAnnotation {
var coordinate : CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(location: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = location
self.title = title
self.subtitle = title
super.init()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder has not been implemented")
}
}
将 self.map.addAnnotation(annotation)
移动到 URLSession.shared.dataTask 完成处理程序。目前图片在 map 添加注释后设置为Dog对象。不要忘记用 OperationQueue.main.addOperation
包装 addAnnotation(_ 调用。
关于ios - URLSession 数据任务后 UIImage 未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46257296/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |