在Swift中将图像发送到ViewController的几个实例

时间:2017-02-04 23:34:58

标签: ios swift uiviewcontroller uiimageview uiimage

我下载了几个"事件"来自我的程序中的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
   }
}

1 个答案:

答案 0 :(得分:0)

将注释存储在单个实例属性中(这意味着您只能访问最后一个属性),将它们存储在数组中,尽管您根本不需要存储它们以响应抽头

var annotations = [CustomPointAnnotation]()

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
    var newAnnotation = CustomPointAnnotation()
    newAnnotation = CustomPointAnnotation()
    newAnnotation.imageName = pic1
    newAnnotation.title = title1
    newAnnotation.subtitle = "Event"
    newAnnotation.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
    self.mapView.addAnnotation(newAnnotation)
    self.annotations.append(newAnnotation)
}

点击的注释视图将传递给您的委托方法,因此您知道它是哪个项目。注释视图的注释属性是您的CustomPointAnnotation实例。

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    let eventPageViewController:EventPageViewController = storyboard?.instantiateViewController(withIdentifier: "EventPage") as! EventPageViewController

    if let annotation = view.annotation as CustomPointAnnotation {
        eventPageViewController.photo = annotation.imageName
    }

    self.present(eventPageViewController, animated: true, completion: nil)
}