gpt4 book ai didi

ios - 将选取的图像发送到另一个 View

转载 作者:行者123 更新时间:2023-11-28 16:18:47 25 4
gpt4 key购买 nike

我正在使用 Mapkit。在左边的标注附件中,有一张小图片,是从用户位置拍摄的。在右边的 calloutaccessory 中,有一个按钮,可以连接到另一个 View Controller ,这样用户就可以看到大尺寸的图像。问题是 - 保存的图像是随机的,将显示在另一个 View 上。

class ImageAnnotation: MKPointAnnotation  {
var image: UIImage?
}

然后在 map View Controller 中它看起来像这样

var imageAnnotations: [ImageAnnotation] = []
var sendImage: UIImageView!

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

guard let annotation = annotation as? ImageAnnotation else {
return nil
}

let identifier = "MyCustomAnnotation"


var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)


if annotationView == nil {

annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true


} else {

annotationView!.annotation = annotation
}

let image = UIImage(named: "advance.png")
let button = UIButton(type: .DetailDisclosure)
button.frame = CGRectMake(0, 0, 30, 30)
button.setImage(image, forState: .Normal)
annotationView?.rightCalloutAccessoryView = button


let detailImage = UIImageView(frame: CGRectMake(0, 0, 50, 50))
detailImage.image = annotation.image
sendImage.image = annotation.image
annotationView?.leftCalloutAccessoryView = detailImage

return annotationView
}

它有效。它在每个注释中显示正确的图像。而“pickedimage”,可以是每一张保存的图片。

override func viewDidAppear(animated: Bool) {
super.viewWillAppear(animated)

for annotation in imageAnnotations {
mapView.addAnnotation(annotation)
}

gettingData()
}

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
{
if (view.annotation is MKPointAnnotation) {
print("Clicked annotation ")
if control == view.rightCalloutAccessoryView {
sendImage.image = UIImage(named: "advance.png")
}
}
}

func buttonPressed (sender: UIButton!) {

let currentImage: UIImage = sender.imageForState(.Normal)!

sendImage.image = currentImage

self.performSegueWithIdentifier("ShowLargeImage", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowLargeImage" {
let goToImageViewController = segue.destinationViewController as! ImageViewController

goToImageViewController.newImage = sendImage.image
}
}

最佳答案

在目标 View Controller 中:

class ImageViewController: UIViewController 
{
@IBOutlet weak var finalImage: UIImageView! // assume that its your imageViewname

var newImage: UIImage! // assume that it is pass Image

override func viewDidLoad() {
super.viewDidLoad()
if let img = newImage
{
finalImage.image = img
}
}
}

在源 View Controller 中:

 //create one Common ImageView
var sendImage: UIImageView!

func buttonPressed (sender: UIButton!) {


self.performSegueWithIdentifier("ShowLargeImage", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowLargeImage" {
let goToImageViewController = segue.destinationViewController as! ImageViewController

goToImageViewController.newImage = sendImage.image // directly pass the image no need of converson
}
}

同时在这里也添加

let detailImage = UIImageView(frame: CGRectMake(0, 0, 50, 50))
detailImage.image = annotation.image
sendImage.image = annotation.image
annotationView?.leftCalloutAccessoryView = detailImage

更新的答案

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) 
{
if (view.annotation is MKPointAnnotation) {
print("Clicked annotation ")
if control == view.rightCalloutAccessoryView {
sendImage.image = UIImage(named: "advance.png")
}
}

}

关于ios - 将选取的图像发送到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38742963/

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