0
私はSwiftで新しく、Swift 3を使用してiOS用のEXIF Viewerを開発しようとしています。現在のところ、データ(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)
}
}