0
我是Swift中的新成員,我正在嘗試使用Swift 3爲iOS開發EXIF查看器。目前,我只能提取非常少量的來自圖像的數據(ColorModel,Depth,PixelHeight,PixelWidth,ProfileName等),但是我的Macbook和iPhone上的相同圖像包含更多的EXIF。Swift 3 - 只能提取非常少量的EXIF數據
故事板1: 用戶從相機膠捲中選擇一張圖片並單擊「使用」按鈕。
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBOutlet weak var viewActiveImage: UIImageView!
@IBOutlet weak var useCamera: UIBarButtonItem!
@IBOutlet weak var useCameraRoll: UIBarButtonItem!
@IBOutlet weak var useActiveImage: UIBarButtonItem!
@IBOutlet weak var clearActiveImage: UIBarButtonItem!
var isNewImage: Bool?
@IBAction func useCamera(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
isNewImage = true
} else {
let alert = UIAlertController(title: "Lookup Failed", message: "No camera is found on device", preferredStyle:UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}
@IBAction func useCameraRoll(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
isNewImage = false
}
}
@IBAction func useActiveImage(_ sender: AnyObject) {
if viewActiveImage.image == nil {
let alert = UIAlertController(title: "Selection Failed", message: "No image selected", preferredStyle:UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
} else {
let storyboard = UIStoryboard(name: "ExifDataViewer", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "exif") as UIViewController
present(vc, animated: true, completion: nil)
}
}
@IBAction func clearActiveImage(_ sender: AnyObject) {
viewActiveImage.image = nil
}
func convertUIImageToNSData(selectionAsUIImage: UIImage) -> NSData {
return (UIImagePNGRepresentation(selectionAsUIImage) as NSData?)!
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
self.dismiss(animated: true, completion: nil)
if mediaType.isEqual(to: kUTTypeImage as String) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
ExifDataViewController.currentActiveImage = convertUIImageToNSData(selectionAsUIImage: image)
viewActiveImage.image = image
if (isNewImage == true) {
UIImageWriteToSavedPhotosAlbum(image, self, #selector(ViewController.image(image:didFinishSavingWithError:contextInfo:)), nil)
}
} else {
let alert = UIAlertController(title: "Selection Failed", message: "Media selected is not an image", preferredStyle:UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}
func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafeRawPointer) {
if error != nil {
let alert = UIAlertController(title: "Save Failed", message: "Failed to save image", preferredStyle:UIAlertControllerStyle.alert)
let cancelAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
}
}
故事板2:處理圖像並在表格中顯示EXIF數據查看
class ExifDataViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
for item in arrayEXIFDictionaryKeys {
extractExifPropertyFromNSData(selectionAsNSData: ExifDataViewController.currentActiveImage, nameOfExifProperty: item as! String)
}
}
override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}
// OUTLETS
// VARIABLES DECLARATION
static var currentActiveImage = NSData()
// ARRAY PROPERTIES
let arrayEXIFDictionaryKeys = [
kCGImagePropertyExifExposureTime,
kCGImagePropertyExifFNumber,
kCGImagePropertyExifExposureProgram,
kCGImagePropertyExifSpectralSensitivity,
kCGImagePropertyExifISOSpeedRatings,
kCGImagePropertyExifOECF,
kCGImagePropertyExifVersion,
kCGImagePropertyExifDateTimeOriginal,
kCGImagePropertyExifDateTimeDigitized,
kCGImagePropertyExifComponentsConfiguration,
kCGImagePropertyExifCompressedBitsPerPixel,
kCGImagePropertyExifShutterSpeedValue,
kCGImagePropertyExifApertureValue,
kCGImagePropertyExifBrightnessValue,
kCGImagePropertyExifExposureBiasValue,
kCGImagePropertyExifMaxApertureValue,
kCGImagePropertyExifSubjectDistance,
kCGImagePropertyExifMeteringMode,
kCGImagePropertyExifLightSource,
kCGImagePropertyExifFlash,
kCGImagePropertyExifFocalLength,
kCGImagePropertyExifSubjectArea,
kCGImagePropertyExifMakerNote,
kCGImagePropertyExifUserComment,
kCGImagePropertyExifSubsecTime,
kCGImagePropertyExifSubsecTimeOriginal,
kCGImagePropertyExifSubsecTimeDigitized,
kCGImagePropertyExifFlashPixVersion,
kCGImagePropertyExifColorSpace,
kCGImagePropertyExifPixelXDimension,
kCGImagePropertyExifPixelYDimension,
kCGImagePropertyExifRelatedSoundFile,
kCGImagePropertyExifFlashEnergy,
kCGImagePropertyExifSpatialFrequencyResponse,
kCGImagePropertyExifFocalPlaneXResolution,
kCGImagePropertyExifFocalPlaneYResolution,
kCGImagePropertyExifFocalPlaneResolutionUnit,
kCGImagePropertyExifSubjectLocation,
kCGImagePropertyExifExposureIndex,
kCGImagePropertyExifSensingMethod,
kCGImagePropertyExifFileSource,
kCGImagePropertyExifCFAPattern,
kCGImagePropertyExifCustomRendered,
kCGImagePropertyExifExposureMode,
kCGImagePropertyExifWhiteBalance,
kCGImagePropertyExifDigitalZoomRatio,
kCGImagePropertyExifFocalLenIn35mmFilm,
kCGImagePropertyExifSceneCaptureType,
kCGImagePropertyExifGainControl,
kCGImagePropertyExifContrast,
kCGImagePropertyExifSaturation,
kCGImagePropertyExifSharpness,
kCGImagePropertyExifDeviceSettingDescription,
kCGImagePropertyExifSubjectDistRange,
kCGImagePropertyExifImageUniqueID,
kCGImagePropertyExifGamma,
kCGImagePropertyExifCameraOwnerName,
kCGImagePropertyExifBodySerialNumber,
kCGImagePropertyExifLensSpecification,
kCGImagePropertyExifLensMake,
kCGImagePropertyExifLensModel,
kCGImagePropertyExifLensSerialNumber] as [Any]
let arrayIndividualImageProperties = [
kCGImagePropertyDPIHeight,
kCGImagePropertyDPIWidth,
kCGImagePropertyPixelHeight,
kCGImagePropertyPixelWidth,
kCGImagePropertyDepth,
kCGImagePropertyOrientation,
kCGImagePropertyIsFloat,
kCGImagePropertyIsIndexed,
kCGImagePropertyHasAlpha,
kCGImagePropertyColorModel,
kCGImagePropertyProfileName] as [Any]
// BACK TO SELECTION SCREEN
@IBAction func returnToMain() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "main") as UIViewController
present(vc, animated: true, completion: nil)
}
// SHARE EXIF RESULTS
@IBAction func shareImageResults() {
}
// GET EXIF PROPERTY
func extractExifPropertyFromNSData(selectionAsNSData: NSData, nameOfExifProperty: String) {
if let imageSource = CGImageSourceCreateWithData(selectionAsNSData as CFData, nil) {
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
let imageDictionary = NSMutableDictionary(dictionary: imageProperties!)
print(imageProperties)
addExifPropertyToTable(displayString: imageDictionary.dictionaryWithValues(forKeys: [nameOfExifProperty as String]))
}
}
// ADD PROPERTY TO TABLE
func addExifPropertyToTable(displayString: [String:Any]) {
//print(displayString)
}
}