Emman Evangelista
Emman Evangelista

Reputation: 11

Storing a Captured Image into a UIImage Array (AVFoundation)

I'm working on a camera application that captures 10 images after a single button press. I want to mean stack this 10 photos using the CIAdditionCompositing CIFilter. However, I can't append the images into an array. When I remove the code that I wrote for the filters, the camera captures and saves 10 images to the Photos library without any errors.

It seems that the problem is with:

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

Since the image is not being stored inside the array.

I am currently getting this error: "Thread 1: Fatal error: Index out of range"

This is the code snippet

@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);


}

Code snippet for CameraController.swift (for 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))")
            }
        }
    }
}

Update: Should I instead rewrite the AVCaptureStillImageOutput into AVCapturePhotoOutput? I tried doing this but it gave me another error stating that I am getting a nil.

If not, is the syntax of appending image to uiImages[] the problem? XCode shows that there are no data inside the uiImages array when I press the button even though the index variable changes

Upvotes: 1

Views: 265

Answers (2)

Zain
Zain

Reputation: 153

Her is more precise answer to your question.

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
}

Then you can Downcase your array

let images = uiImagesMutableArray as! [UIImage]

Upvotes: 1

Muhammad Awais Jamil
Muhammad Awais Jamil

Reputation: 406

You should start your Index from 0. or make define the array count before. Find comments of "// Here is your solution."

var index = 1 // Array's index should start form 0.
var index = 0 // Here is your solution.
while index < 11{
        self.cameraController.captureStillImage { (image, metadata) -> Void in
            self.view.layer.contents = image
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

            uiImages.insert(image, at: index) // Here is your solution.
            uiImages[index] = image //I think i'm having problems here
    }
    index = index + 1

}

Upvotes: 0

Related Questions