相机照片的时间和日期

时间:2015-10-23 12:35:45

标签: ios swift datetime image-capture

使用imagepickercontroller从相机拍摄后,如何获取照片的时间和日期? 我试过环顾四周,但没有设法找到解决方案。

以下是我的代码,我可以在哪里调用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)
            }


        })

    })

2 个答案:

答案 0 :(得分:0)

UIImagePickerControllerDelegate包含以下方法:- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

info字典具有一组预定义的键,这些键将返回与所选图像关联的众多对象。你想要的是UIImagePickerControllerMediaMetadata。这将返回一个字典,其中包含拍摄图像的日期/时间。

有关可用密钥的详细信息,请参阅:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIImagePickerControllerDelegate_Protocol/index.html#//apple_ref/doc/constant_group/Editing_Information_Keys

答案 1 :(得分:0)

您可以简单地将let currentDate = NSDate()放入captureStillImageAsynchronouslyFromConnection功能的完成功能块中,以获取图像捕获回叫的日期和时间!