gpt4 book ai didi

ios - 在 Swift 中将图像发送到 ViewController 的多个实例

转载 作者:行者123 更新时间:2023-11-30 12:42:42 25 4
gpt4 key购买 nike

我在程序中从 CloudKit 下载了几个“事件”。每一张都包含图片、标题和位置。后两个立即使用,没有问题。仅当用户单击注释上的信息按钮(使用单独的 View Controller 显示图片)时才能看到该图像。我在使用 ViewController 之前无法将图像传递到 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
}
}

最佳答案

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

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

点击的注释 View 将传递给您的委托(delegate)方法,因此您知道它是哪个项目。注释 View 的注释属性是您的 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)
}

关于ios - 在 Swift 中将图像发送到 ViewController 的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42046901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com