Skip to content

Instantly share code, notes, and snippets.

@NikhilManapure
Created October 13, 2017 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikhilManapure/259e4d7a5bb53636ad378bfb4eec9062 to your computer and use it in GitHub Desktop.
Save NikhilManapure/259e4d7a5bb53636ad378bfb4eec9062 to your computer and use it in GitHub Desktop.
if let outputs = camera.captureSession.outputs {
for output in outputs {
if let output = output as? AVCaptureStillImageOutput {
if let videoConnection = output.connection(withMediaType: AVMediaTypeVideo) {
output.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (imageDataSampleBuffer, error) in
if let imageDataSampleBuffer = imageDataSampleBuffer {
let data: Data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
let rawMetadata = CMCopyDictionaryOfAttachments(nil, imageDataSampleBuffer, CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))
let metadata = CFDictionaryCreateMutableCopy(nil, 0, rawMetadata) as NSMutableDictionary
let exifData = metadata.value(forKey: kCGImagePropertyExifDictionary as String) as? NSMutableDictionary
exifData?.setValue(Date().toString(withFormat: "yyyy:MM:dd HH:mm:ss"), forKey: kCGImagePropertyExifDateTimeDigitized as String)
metadata.setValue(exifData, forKey: kCGImagePropertyExifDictionary as String)
metadata.setValue(1, forKey: kCGImagePropertyOrientation as String)
metadata.setValue(self.camera.inputCamera.iso, forKey: kCGImagePropertyExifISOSpeed as String)
if let image = UIImage(data: data) {
// Use Image
} else {
// Error
}
} else if let error = error {
onError(error)
} else {
onError(NSError(domain: "Something is unusually wrong", code: 0, userInfo: nil))
}
})
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment