Lakshmi Narayanan
Lakshmi Narayanan

Reputation: 5342

UIImageWriteToSavedPhotosAlbum error : use of unresolved identifier

I am trying to save the images taken from the cam. The following code has a function that takes n number of images as per the photocount. The code snippet I wrote within the function is to save each of these images.

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()
        }
    }

}

I have tagged the code I added to achieve this. The error faced is with the completion target identifier.

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

I am new to swift and Xcode, and I might be treating it like traditional c++/java. The mistake has to be trivial. Can anyone point out what exactly should be done here?

Upvotes: 1

Views: 742

Answers (2)

Kamran
Kamran

Reputation: 15238

Move this method image_new out of photoOutput as below,

//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()
        }
    }
}

Upvotes: 0

Gereon
Gereon

Reputation: 17844

You need to make a couple of changes:

Pull out func image_new(_image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) so that it is a member of the class, not a function within a function.

Change the signature to func image_new(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) (i.e. make the first parameter unnamed)

Make the function visible to the Objective-C runtime: @objc func image_new(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer)

Upvotes: 2

Related Questions