Øyvind
Øyvind

Reputation: 13

How do I set an image so I can save it in swift 2

Hi I am writing a camera app where the picture will be saved to the cameraroll when you press the photobutton without displaying it. I am almost done, but I have a problem with a piece of code that should work with swift, but don't work with swift 2.

  func didTakePhoto() {
    if let videoConection = stillImageOutput2?.connectionWithMediaType(AVMediaTypeVideo){
        videoConection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput2?.captureStillImageAsynchronouslyFromConnection(videoConection, completionHandler: { (sampleBuffer, ErrorType) -> Void in
            if sampleBuffer != nil {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProviderCreateWithCFData(imageData)
                let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, .RenderingIntentDefault)

                var savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)

            }
        })
    }
}

@IBAction func takePhotoBtn(sender: UIButton) {
    didTakePhoto()
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
}

When I try to use the image i just created in the function that saves it, it doesn't work. How can I fix this?

Upvotes: 0

Views: 307

Answers (2)

matt
matt

Reputation: 536027

Your code is structured like this:

func didTakePhoto() {
     // ...
     var savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
}
@IBAction func takePhotoBtn(sender: UIButton) {
    didTakePhoto()
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
}

So savedImage is declared locally in, and therefore is entirely confined to, the world of didTakePhoto(). In the world of takePhotoBtn, no savedImage exists — and thus you get a compiler error.

You have two choices. You can declare savedImage at a higher level that both methods can see:

var savedImage:UIImage!
func didTakePhoto() {
     // ...
     savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
}
@IBAction func takePhotoBtn(sender: UIButton) {
    didTakePhoto()
    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil)
}

Or, you can have didTakePhoto return savedImage as its result:

func didTakePhoto() -> UIImage {
     // ...
     let savedImage = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
     return savedImage
}
@IBAction func takePhotoBtn(sender: UIButton) {
    UIImageWriteToSavedPhotosAlbum(didTakePhoto(), nil, nil, nil)
}

Upvotes: 1

Stefan
Stefan

Reputation: 5461

I'm using this snippet in my project:

func saveImage(image: UIImage) 
   let destinationPath = documentsPath.stringByAppendingPathComponent("test.png")
   UIImagePNGRepresentation(rotateImage(image))!.writeToFile(destinationPath, atomically: true)

 }


func rotateImage(image: UIImage) -> UIImage {
    print(image.imageOrientation.hashValue )
    if (image.imageOrientation == UIImageOrientation.Up ) {

        return image //UIImage(CGImage: image, scale: 0.5, orientation: UIImageOrientation.Up)
    }

    UIGraphicsBeginImageContext(image.size)

    image.drawInRect(CGRect(origin: CGPoint.zero, size: image.size))
    let copy = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()
    return copy
}

Upvotes: 0

Related Questions