首页 > 解决方案 > 将捕获的图像存储到 UIImage 数组 (AVFoundation)

问题描述

我正在开发一个相机应用程序,该应用程序在按下一个按钮后捕获 10 张图像。我的意思是使用 CIAdditionCompositing CIFilter 堆叠这 10 张照片。但是,我无法将图像附加到数组中。当我删除为过滤器编写的代码时,相机会捕获 10 张图像并将其保存到照片库中,而不会出现任何错误。

似乎问题出在:

 uiImages.insert(image, at: index) //this might be the cause of the error

由于图像没有存储在数组中。

我目前收到此错误:“线程 1:致命错误:索引超出范围”

这是代码片段

@IBAction func handleShutterButton(sender: UIButton) {
    var uiImages: [UIImage] = []
    var ciImages: [CIImage] = []
    var resultImages: [CIImage] = []


    let filter = CIFilter(name: "CIAdditionCompositing")!
    filter.setDefaults()

    var index = 0
    while index < 10{
          self.cameraController.captureStillImage { (image, metadata) -> Void in
          self.view.layer.contents = image
          UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

          uiImages.insert(image, at: index) //this might be the cause of the error
        }
        index = index + 1

    }


    ciImages[0] = CIImage(image: uiImages[0])!
    ciImages[1] = CIImage(image: uiImages[1])!
    ciImages[2] = CIImage(image: uiImages[2])!
    ciImages[3] = CIImage(image: uiImages[3])!
    ciImages[4] = CIImage(image: uiImages[4])!
    ciImages[5] = CIImage(image: uiImages[5])!
    ciImages[6] = CIImage(image: uiImages[6])!
    ciImages[7] = CIImage(image: uiImages[7])!
    ciImages[8] = CIImage(image: uiImages[8])!



    filter.setValue(ciImages[0], forKey: "inputImage")
    filter.setValue(ciImages[1], forKey: "inputBackgroundImage")

    resultImages[0] = filter.outputImage!


    filter.setValue(ciImages[2], forKey: "inputImage")
    filter.setValue(resultImages[0], forKey: "inputBackgroundImage")

    resultImages[1] = filter.outputImage!


    filter.setValue(ciImages[3], forKey: "inputImage")
    filter.setValue(resultImages[1], forKey: "inputBackgroundImage")

    resultImages[2] = filter.outputImage!


    filter.setValue(ciImages[4], forKey: "inputImage")
    filter.setValue(resultImages[2], forKey: "inputBackgroundImage")

    resultImages[3] = filter.outputImage!


    filter.setValue(ciImages[5], forKey: "inputImage")
    filter.setValue(resultImages[3], forKey: "inputBackgroundImage")

    resultImages[4] = filter.outputImage!


    filter.setValue(ciImages[6], forKey: "inputImage")
    filter.setValue(resultImages[4], forKey: "inputBackgroundImage")

    resultImages[5] = filter.outputImage!


    filter.setValue(ciImages[7], forKey: "inputImage")
    filter.setValue(resultImages[5], forKey: "inputBackgroundImage")

    resultImages[6] = filter.outputImage!


    filter.setValue(ciImages[8], forKey: "inputImage")
    filter.setValue(resultImages[6], forKey: "inputBackgroundImage")

    resultImages[7] = filter.outputImage!


    filter.setValue(ciImages[9], forKey: "inputImage")
    filter.setValue(resultImages[7], forKey: "inputBackgroundImage")

    resultImages[8] = filter.outputImage!


    let finalImage = UIImage(ciImage: resultImages[8])
    UIImageWriteToSavedPhotosAlbum(finalImage, nil, nil, nil);


}

CameraController.swift 的代码片段(用于 captureStillImage)

func captureSingleStillImage(completionHandler handler: @escaping ((_ image:UIImage, _ metadata:NSDictionary) -> Void)) {
    sessionQueue.async() { () -> Void in

        let connection = self.stillCameraOutput.connection(with: AVMediaType.video)

        connection?.videoOrientation = AVCaptureVideoOrientation(rawValue: UIDevice.current.orientation.rawValue)!

        self.stillCameraOutput.captureStillImageAsynchronously(from: connection!) {
            (imageDataSampleBuffer, error) -> Void in


            if error == nil {



                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer!)

                let metadata = CMCopyDictionaryOfAttachments(allocator: nil, target: imageDataSampleBuffer!, attachmentMode: CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))

                if let metadata = metadata, let image = UIImage(data: imageData!) {
                    DispatchQueue.main.async() { () -> Void in
                        handler(image, metadata)
                    }
                }
            }
            else {
                NSLog("error while capturing still image: \(String(describing: error))")
            }
        }
    }
}

更新:我是否应该将 AVCaptureStillImageOutput 重写为 AVCapturePhotoOutput?我试过这样做,但它给了我另一个错误,说明我得到了一个零。

如果不是,将图像附加到 uiImages[] 的语法是否有问题?当我按下按钮时,XCode 显示 uiImages 数组中没有数据,即使索引变量发生了变化

标签: iosarraysswiftavfoundationswift4

解决方案


她更准确地回答了你的问题。

var uiImagesMutableArray = NSMutableArray.init(capacity: 10) //All you need is NSMutableArray
for i in 0...9 {
       self.cameraController.captureStillImage {(image, metadata) -> Void in
       self.view.layer.constents = image
       UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
       uiImagesMutableArray.insert(image, at: i)
       i += 1
}

然后你可以缩小你的数组

let images = uiImagesMutableArray as! [UIImage]

推荐阅读