-2

How do I get the time and date of the photo after shooting it from my camera with imagepickercontroller? I've tried looking around but haven't managed to find a solution.

Below is my code, where can I call didfinishpickingmediawithinfo

dispatch_async(self.sessionQueue, {

        self.stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation

        self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo), completionHandler: {
            (imageDataSampleBuffer: CMSampleBuffer!, error: NSError!) in

            if error == nil {
                let data:NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                let image:UIImage = UIImage( data: data)!

                let libaray:ALAssetsLibrary = ALAssetsLibrary()
                let orientation: ALAssetOrientation = ALAssetOrientation(rawValue: image.imageOrientation.rawValue)!
                libaray.writeImageToSavedPhotosAlbum(image.CGImage, orientation: orientation, completionBlock: nil)

                //pass the image to preview view controller for confirmation
                self.snappedImage = image
                self.performSegueWithIdentifier("previewPhoto", sender: self)


            }else{
                //                    print("Did not capture still image")
                print(error)
            }


        })

    })
KTPatel
  • 1,212
  • 4
  • 19
  • 24
Happiehappie
  • 1,084
  • 2
  • 13
  • 26

2 Answers2

0

The UIImagePickerControllerDelegate includes the following method: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

The info dictionary has a set of pre-defined keys that will return numerous objects associated with the image that was selected. The one you want is UIImagePickerControllerMediaMetadata. This will return a dictionary that includes the date/time of when the image was taken.

For more information about the keys available to you see: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html#//apple_ref/doc/constant_group/Editing_Information_Keys

Peter Foti
  • 5,526
  • 6
  • 34
  • 47
0

You can simply put let currentDate = NSDate() in the completion block of captureStillImageAsynchronouslyFromConnection function to get the date and time you get the image capturing call back!

Abhinav
  • 37,684
  • 43
  • 191
  • 309