我正在开发一个相机应用程序,该应用程序只需按一次按钮即可捕获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数组内也没有数据
答案 0 :(得分:1)
她是您问题的更精确答案。
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]
答案 1 :(得分:0)
您应该从0开始索引。或者在之前定义数组计数。 查找“ //这是您的解决方案”的注释。
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
}