Alexandr Smushko Alexandr Smushko - 10 months ago 61
iOS Question

Adding captured image to next view Swift 3 Xcode 8

I'm using AVFoundation for capturing. And I want to add captured image to next view's UIImageView. I have a code, that lets me to add one photo to one UIImageView:

func saveToCamera() {

if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo) {

stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (CMSampleBuffer, Error) in
if let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(CMSampleBuffer) {

if let cameraImage = UIImage(data: imageData) {

UIImageWriteToSavedPhotosAlbum(cameraImage, nil, nil, nil)
self.scan = cameraImage //recording captured image to variable


Prepare for segue code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "MainSegue"){
let yourNextViewController = (segue.destination as! MainView)
yourNextViewController.scan = scan

Next view:

var scan: UIImage!
@IBOutlet weak var imgScan: UIImageView!

override func viewDidLoad() {

imgScan.image = scan

It is primitive and works perfectly, but only for one image. But I want to make up to six photos and add them to six ImageViews. How can I do it?

Answer Source

I expect the key thing here will be to change this:

var scan: UIImage!

to this:

var scan: [UIImage] = []()

Now you've got an array you can append to, adding each of your images in prepare. Of course you will also have to change the code in the second view controller to look at how many images you've got and assign them to your various image views, but that's a trivial change to make.