将Uiimages传递给视图控制器

时间:2017-03-17 10:42:16

标签: ios iphone swift xcode swift3

所以我有一个名为Paparazzo的自定义相机,从相机拍摄的图像存储在名为ImageSource的自定义数组中。现在我声明我将ImageSource从摄像机视图控制器传递到我的第二个视图控制器,但我意识到我的图像在第二个视图控制器上出现的位置为零。如何通过segue传递从图像声明为ImageSource的所有图像。那是我的问题。这是我的代码。

private var photos = [ImageSource]()

@IBAction private func showMediaPicker() {
    var theme = PaparazzoUITheme()
    theme.shutterButtonColor = UIColor.blue.cgColor
    let assemblyFactory = Paparazzo.AssemblyFactory(theme: theme)
    let assembly = assemblyFactory.mediaPickerAssembly()
    let mediaPickerController = assembly.module(
        items: [],
        selectedItem: nil,
        maxItemsCount: 20,
        cropEnabled: true,
        cropCanvasSize: CGSize(width: 1280, height: 960),
        configuration: { [weak self] module in
            weak var module = module

            module?.setContinueButtonTitle("Done")

            module?.onFinish = { mediaPickerItems in
                // storing picked photos in instance var and updating UI
                self?.photos = mediaPickerItems.map { $0.image }
                self?.updateUI()
                self?.performSegue(withIdentifier: "ShowNow", sender: self?.photos)
            }
            module?.onCancel = {
                module?.dismissModule()
            }
        }
    )

    navigationController?.pushViewController(mediaPickerController, animated: true)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "ShowNow") {
        if let pivc: view2ViewController? = segue.destination as? view2ViewController,
            let images = sender as? [UIImage] {
            pivc?.image2 = images
        }
    }
}

3 个答案:

答案 0 :(得分:0)

你可以简单地通过保存UIImage来保存[NSUserDefault standardUserDefault]中的图像setobject:image valueforkey:@" img" ]并通过[NSUserDefault standardUserDefault] valueforkey得到它:@" img"或者您可以通过创建UIImage的全局变量来传递它并传递给另一个viewcontroller。

使用此代码保存图像对于PNG:

[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"img"];

使用此代码保存图像对于JPEG:

[[NSUserDefaults standardUserDefaults] UIImageJPEGRepresentation(image) forKey:@"img"];

并使用此代码将其取回

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"img"]; UIImage* image = [UIImage imageWithData:imageData];

答案 1 :(得分:0)

你的第一个ViewController里面有一个带有ImageSource的数组。 您的目标ViewController应该具有类似的数组(例如:var passedPhotos: [ImageSource]?)。

第一个VC中的prepare(for segue: UIStoryboardSegue, sender: Any)方法应如下所示:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "YourSegueIdentifier") {
        if let secondVC: YourSecondViewController? = segue.destination as? YourSecondViewController,
            if let photosToPass = photos {
                secondVC?.passedPhotos = photosToPass 
            }
        }
    }
}

答案 2 :(得分:0)

首先在secondVC中创建一个数组,如

 var arrImages =  Array()

然后在你的currentVC中做这些事情

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "ShowNow") {
             let pivc: view2ViewController? = segue.destination as? view2ViewController

                pivc?.arrImages = images
// here images is the array of images

        }
    }