ios - 将捕获的图像添加到下一个 View Swift 3 Xcode 8

标签 ios swift swift3 xcode8 capture

我正在使用 AVFoundation 进行捕获。我想将捕获的图像添加到下一个 View 的 UIImageView。我有一个代码,可以让我将一张照片添加到一个 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
                }

            }
        })
    }
}

准备segue代码:

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

下一 View :

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

override func viewDidLoad() {
    super.viewDidLoad()

    imgScan.image = scan
}

它很原始,但效果很好,但只适用于一张图片。但是我最多要补六张照片,然后将它们添加到六个 ImageView 中。我该怎么做?

最佳答案

我希望这里的关键是改变这一点:

var scan: UIImage!

为此:

var scan: [UIImage] = []()

现在您已经有了一个可以append 的数组,在prepare 中添加您的每个图像。当然,您还必须更改第二个 View Controller 中的代码,以查看您有多少图像并将它们分配给各种 ImageView ,但这是一个微不足道的更改。

关于ios - 将捕获的图像添加到下一个 View Swift 3 Xcode 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40406685/

相关文章:

date - 使用 SwiftDate 的日期之间的小时数

ios - 逐个删除字符后判断UITextfield是否为空

Xcode 在 Swift 2.2 和 Swift 3.0 之间切换

ios - accountsWithAccountType 返回空数组

ios - 使用 beginUpdates/endUpdates 时,UISearchBar 从 tableHeaderView 中消失

objective-c - UIView subview 中的 setNeedsLayout

iOS CGColor 与 UIColor

swift - 对 superview 的约束不起作用

ios - navigationAction.navigationType == .linkActivated 什么时候执行?

iphone - 在符号之间加入字符串的一部分?