在 Swift 中将图像发送到 ViewController 的多个实例 [英] Sending Images to Several Instances of a ViewController in Swift

查看:16
本文介绍了在 Swift 中将图像发送到 ViewController 的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的程序中从 CloudKit 下载了几个事件".每一个都包含一张图片、标题和位置.后两个直接用,没有问题.仅当用户单击使用单独视图控制器显示图片的注释上的信息按钮时,才能看到图像.我在使用图像之前无法将图像传递给 ViewController,同时还要将每个事件的图片分开.

I download several "events" from CloudKit in my program. Each one contains a picture, title, and location. The second two are used right away and there is no problem. The image is only seen if the user clicks on the info button on the annotation that shows the picture using a separate view controller. I am having trouble getting the images to be passed to the ViewController before it is used while also keeping each event's picture separate.

这里是从 CloudKit 查询每条记录并放入 for 循环的地方,然后发送到下面的方法:

Here is where each record is queried from CloudKit and put into a for loop that send then to the method below:

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

这里是变量被分配并用于创建点注释的地方(它是一个包含图像的自定义注释):

Here is where the variables are assigned and used to create the point annotation (it is a custom one that includes an image):

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 的函数:

Here is the function that makes EventPage appear when the info button on an MKMapAnnotation is clicked:

    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:

Here is the ViewController for EventPage:

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

推荐答案

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

Intead of storing your annotations in a single instance property (which means that you only have access to the last one), store them in an array, although you don't need to store them at all in order to respond to taps.

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 实例.

The annotation view that is tapped is passed to your delegate method, so you know which item it is. The annotation view's annotation property is your CustomPointAnnotation instance.

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

这篇关于在 Swift 中将图像发送到 ViewController 的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆