【问题标题】:Sending Images to Several Instances of a ViewController in Swift在 Swift 中将图像发送到 ViewController 的多个实例
【发布时间】: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


【解决方案1】:

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

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)
}

【讨论】:

  • 一行 如果让 annotation = view.annotation as! CustomPointAnnotation { eventPageViewController.photo = annotation.imageName } 我收到错误消息:“条件绑定的初始化程序必须具有可选类型,而不是 'CustomPointAnnotation'”
  • 我添加之前的错误!是:“/'MKAnnotation?'不能转换为“CustomPointAnnotation”;您的意思是使用“as!”吗?强迫沮丧?” @Paulw11
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 2012-05-19
相关资源
最近更新 更多