UIImageWriteToSavedPhotosAlbum错误:使用无法解析的标识符

时间:2019-01-20 08:13:32

标签: ios swift xcode

我正在尝试保存从凸轮拍摄的图像。以下代码具有的功能是按照光计数获取 n 个图像。我在函数中编写的代码段用于保存每个图像。

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto 
photo: AVCapturePhoto, error: Error?) 
{
    if let imageData = photo.fileDataRepresentation() 
    {
        image = UIImage(data: imageData)

        //the code added/////////

        //save the images for test and debug
        UIImageWriteToSavedPhotosAlbum(image!,self,#selector(image_new(_:didFinishSavingWithError:contextInfo:)),nil)
        //end

        //target function after saving the images
        func image_new(_image:UIImage,didFinishSavingWithError error : Error?,contextInfo : UnsafeRawPointer)
        {
            if let error = error
            {
                let ac = UIAlertController(title: "Save Error",message: error.localizedDescription, preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "Ok", style: .default))
                present(ac,animated: true)
            }
            else
            {
                let ac = UIAlertController(title: "Saved",message: "Your pic has been saved",preferredStyle: .alert)
                ac.addAction(UIAlertAction(title: "Ok", style: .default))
                present(ac,animated: true)

            }
        }

        // the code addedd ends /////

        self.images.append(image!)
        //self.images.append((image?.resized(toWidth: 1200))!)
        let seconds = (currentCamera?.exposureDuration.seconds)!
        self.times.append(Float(seconds * seconds))

        self.takenPhoto = true

        if self.images.count >= self.photoCount 
        {
            self.msg.text = ""
            self.stopRunningCaptureSession()
            self.indicator.startAnimating()
        }
    }

}

我已经标记了为实现此目的而添加的代码。面临的错误是完成目标标识符。

 Use of unresolved identifier 'image_new(_:didFinishSavingWithError:contextInfo:)'

我对swift和Xcode并不陌生,我可能会像对待传统的c ++ / java一样对待它。这个错误必须是微不足道的。谁能指出在这里应该做什么?

2 个答案:

答案 0 :(得分:1)

您需要进行一些更改:

拔出func image_new(_image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer),使其成为类的成员,而不是函数中的函数。

将签名更改为func image_new(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer)(即,使第一个参数不命名)

使函数对Objective-C运行时可见:@objc func image_new(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer)

答案 1 :(得分:0)

如下所示从image_new中移出此方法photoOutput

//target function after saving the images
func image_new(_ image:UIImage,didFinishSavingWithError error : Error?,contextInfo : UnsafeRawPointer)
{
    if let error = error
    {
        let ac = UIAlertController(title: "Save Error",message: error.localizedDescription, preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "Ok", style: .default))
        present(ac,animated: true)
    }
    else
    {
        let ac = UIAlertController(title: "Saved",message: "Your pic has been saved",preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "Ok", style: .default))
        present(ac,animated: true)

    }
}

func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto 
photo: AVCapturePhoto, error: Error?) 
{
    if let imageData = photo.fileDataRepresentation() 
    {
        image = UIImage(data: imageData)

        //the code added/////////

        //save the images for test and debug
        UIImageWriteToSavedPhotosAlbum(image,self,#selector(image_new(_:didFinishSavingWithError:contextInfo:)),nil)
        //end

        // the code addedd ends /////

        self.images.append(image!)
        //self.images.append((image?.resized(toWidth: 1200))!)
        let seconds = (currentCamera?.exposureDuration.seconds)!
        self.times.append(Float(seconds * seconds))

        self.takenPhoto = true

        if self.images.count >= self.photoCount 
        {
            self.msg.text = ""
            self.stopRunningCaptureSession()
            self.indicator.startAnimating()
        }
    }
}