2015-08-08 18 views
6

Informacja: Korzystanie z funkcji Swift i CGImageSourceCreateWithURL.Swift z dostępem do EXIF ​​Słownik

Próbuję wczytać plik z adresu URL, a następnie edytować słownik zawierający wszystkie dane z tego konkretnego zdjęcia.

To jest kod z pliku .swift.

let url = NSURL(string: "http://jwphotographic.co.uk/Images/1.jpg") 
    let imageSource = CGImageSourceCreateWithURL(url, nil) 
    let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary 

    println(imageProperties) 

    //this is an example 
    let aperture = imageProperties[kCGImagePropertyGPSLatitude] as! NSNumber! 

    /* 
    //these are all being defined as nil 
    //Load the ones from the exif data of the file 
    let lensUsed = imageProperties[kCGImagePropertyExifFocalLength] 
    let aperture = imageProperties[kCGImagePropertyExifApertureValue] as! 
    let isoSpeed = imageProperties[kCGImagePropertyExifISOSpeedRatings] as! NSNumber 
    let latitude = imageProperties[kCGImagePropertyGPSLatitude] as! NSNumber 
    let longitude = imageProperties[kCGImagePropertyGPSLongitude] as! NSNumber 
    let shutterSpeed = imageProperties[kCGImagePropertyExifShutterSpeedValue] as! NSNumber 
    let cameraName = imageProperties[kCGImagePropertyExifBodySerialNumber] as! NSNumber 
    */ 

    println(aperture) 

Chociaż właściwości graficzne drukuje wszystkie dane, jak można było oczekiwać, bez względu na to, co mam attmpted wyodrębnić ze słownika imageProperties - jest zawsze zwracane jako null - takich jak „otwór” w przykładzie. Właściwość imageProperties drukuje jako;

[{TIFF}: { 
    Artist = JOHN; 
    Copyright = "[email protected]"; 
    DateTime = "2015:07:31 21:07:05"; 
    Make = Canon; 
    Model = "Canon EOS 7D Mark II"; 
    ResolutionUnit = 2; 
    Software = "Adobe Photoshop Lightroom 6.0 (Macintosh)"; 
    XResolution = 72; 
    YResolution = 72; 
}, {IPTC}: { 
    Byline =  (
     JOHN 
    ); 
    CopyrightNotice = etc.. etc.. 

Zrobiłem wiele badań i testów, a ja po prostu nie może się zorientować, co robię źle, aby uzyskać dostęp do elementów w tym słowniku - mógłby ktoś dać mi przykład jak bym ustawić zmienną jako Element "Model" w słowniku?

Dzięki za wszelką pomoc, John

Odpowiedz

6

Aby dostać się do parametrów Exif wykonaj następujące czynności:

let filePath_ = "file:///Users/pfernandes/Documents/softwareDevelopment/PhotoLine/TestData/IMG_1243.JPG" 
    let fileURL = NSURL(string:filePath_) 
    let myImage = CGImageSourceCreateWithURL(fileURL!,nil) 

    let imageDict = CGImageSourceCopyPropertiesAtIndex(myImage!, 0, nil)! as NSDictionary; 
    let tiffModel_ = imageDict.value(forKey: "{TIFF}") 

    let cameraMake = (tiffModel_ as AnyObject).value(forKey: kCGImagePropertyTIFFMake as String) 
    let cameraModel = (tiffModel_ as AnyObject).value(forKey: kCGImagePropertyTIFFModel as String) 

    let exifDict_ = imageDict.value(forKey: "{Exif}") as! NSDictionary 
    let dateTimeOriginal = exifDict_.value(forKey:kCGImagePropertyExifDateTimeOriginal as String) as! NSString 
    let exposure   = exifDict_.value(forKey:kCGImagePropertyExifExposureTime as String) 

    print ("\(String(describing: cameraMake)) \(String(describing: cameraModel)) \(dateTimeOriginal) \(exposure!)") 
+0

Czy istnieje sposób uzyskania informacji o kamerze z biblioteki zdjęć? Dzięki –

+1

Tak, zredagowałem swoją odpowiedź, aby pokazać przykład jak to zrobić w najnowszym Swift/Xcode – Cortex

9

w Swift 3.0 Znalazłem następujące rozwiązanie

let url = NSURL(string: "http://jwphotographic.co.uk/Images/1.jpg") 
let imageSource = CGImageSourceCreateWithURL(url, nil) 
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as Dictionary? 
let exifDict = imageProperties?[kCGImagePropertyExifDictionary] 

Teraz można uzyskać dostęp exif-Tags, na przykład:

let dateTimeOriginal = exifDict?[kCGImagePropertyExifDateTimeOriginal] 
Swift.print("dateTimeOriginal: \(dateTimeOriginal)") 

Otrzymasz opcjonalne wartości i musisz sprawdzić, czy są wartości lub zero. Aby uzyskać listę dostępnych stałych właściwości, spójrz na numer apple documentation.

Powiązane problemy