0

I am trying to send a photo with the app I am working on, I have made the app to take a photo and then when you tap on send it would send the photo you just took to send it through mail.

But I don't know how to convert the photo that is of type AVCaptureStillImageOutput to UIImage and store it in a NSData in order to use it as an attachment in addAttachmentData.

I tried to do this:

let data: NSData = UIImagePNGRepresentation(image: stillImageOutput)

I have this function

func doSomethingWithImage(image: UIImage) {
        // do something here
        mc.addAttachmentData(UIImageJPEGRepresentation(UIImage(named: imageAsUIIMage)!, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "backupOne.jpeg")
    }

But It shows me an error, "Use of unresolved identifier 'imageAsUIIMage'

I want to get the UIImage in order to send it through an e-mail.

Pixele9
  • 422
  • 8
  • 25

1 Answers1

1

You can get capture an image and save it to file and convert it to UIImage using stillImageOutput.captureStillImageAsynchronouslyFromConnection()

// setup code
stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]

captureSession.startRunning()

let connection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)
    stillImageOutput.captureStillImageAsynchronouslyFromConnection(connection) { (sampleBuffer, error) in
    // NSData of jpeg data. Save to file and add as email attachment.
    let jpegData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)

    dispatch_async(dispatch_get_main_queue(), {
        self.doSomethingWithJPEGImageData(jpegData!)
    })
}

// later on
func doSomethingWithJPEGImageData(jpegData: NSData) {
    mc.addAttachmentData(jpegData, mimeType: "image/jpeg", fileName: "backupOne.jpeg")
}
Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • Thanks! Where should I put that? – Pixele9 Oct 09 '15 at 01:18
  • After you create your `AVCaptureSessionPresetPhoto` capture session and add the `stillImageOutput` to it. – Rhythmic Fistman Oct 09 '15 at 01:20
  • Did that fix the problem? – Rhythmic Fistman Oct 20 '15 at 01:39
  • Sorry! Is because the camera I had in my app stopped working so I have to fix it in order to test this code! – Pixele9 Oct 20 '15 at 22:17
  • Hey! Thanks for the help, but how can I use it in the "addAttachmentData" when I try to use it it says: "Use of unresolved identifier 'imageAsUIImage" – Pixele9 Nov 01 '15 at 22:31
  • Is this a new question? If so, start a new question. Is your old question answered? – Rhythmic Fistman Nov 02 '15 at 00:32
  • NO, this isn't a new question, I don't know how to take out the imageAsUIImage of the scope to use it. – Pixele9 Nov 02 '15 at 01:19
  • ah, I added a method in which you can do something with the image – Rhythmic Fistman Nov 02 '15 at 01:25
  • Sorry but I am kind of a noob and I don't know what to do next with that function, I have this: func doSomethingWithImage(image: UIImage) { // do something here mc.addAttachmentData(UIImageJPEGRepresentation(UIImage(named: imageAsUIIMage)!, CGFloat(1.0))!, mimeType: "image/jpeg", fileName: "backupOne.jpeg") } But it shows me an error – Pixele9 Nov 02 '15 at 01:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93938/discussion-between-pixele9-and-rhythmic-fistman). – Pixele9 Nov 02 '15 at 01:49