【发布时间】:2017-02-04 23:34:57
【问题描述】:
我在我的程序中从 CloudKit 下载了几个“事件”。每一个都包含一张图片、标题和位置。后两个立即使用,没有问题。仅当用户单击使用单独的视图控制器显示图片的注释上的信息按钮时,才能看到图像。我无法在使用 ViewController 之前将图像传递给它,同时还要保持每个事件的图片分开。
这里是从 CloudKit 查询每条记录并放入一个 for 循环,然后发送到下面的方法:
func loadEvent(_ completion: @escaping (_ error:NSError?, _ records:[CKRecord]?) -> Void)
{
//...record is downloaded from cloudkit
for record in records
{
if let asset = record["Picture"] as? CKAsset,
let data = NSData(contentsOf: asset.fileURL),
let image1 = UIImage(data: data as Data)
{
self.drawEvents(record["LocationF"] as! CLLocation, title1: record["StringF"] as! String, pic1: image1)
}
}
}
这是分配变量并用于创建点注释的位置(它是包含图像的自定义注释):
func drawEvents(_ loc: CLLocation, title1: String, pic1: UIImage)
{
mapView.delegate = self
let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
self.pointAnnotation1 = CustomPointAnnotation()
self.pointAnnotation1.imageName = pic1
self.pointAnnotation1.title = title1
self.pointAnnotation1.subtitle = "Event"
self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil)
self.mapView.addAnnotation(self.pinAnnotationView.annotation!)
}
这是在单击 MKMapAnnotation 上的信息按钮时使 EventPage 出现的函数:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let eventPageViewController:EventPageViewController = storyboard?.instantiateViewController(withIdentifier: "EventPage") as! EventPageViewController
self.present(eventPageViewController, animated: true, completion: nil)
}
这是 EventPage 的 ViewController:
class EventPageViewController: UIViewController {
@IBOutlet weak var eventPic: UIImageView!
var photo: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
let firstViewController:FirstViewController = storyboard?.instantiateViewController(withIdentifier: "Home") as! FirstViewController
photo = firstViewController.pointAnnotation1.imageName
//photo is nil
eventPic.image = photo
}
}
【问题讨论】:
-
将您的项目保存到数组而不是单个变量中。
-
但是每张图片如何知道何时传递到事件页面。每一个都会不同
-
被点击的注释视图被传递给你的委托方法,所以你知道它是哪个项目。注释视图的
annotation属性是您的CustomPointAnnotation实例。
标签: ios swift uiviewcontroller uiimageview uiimage