2015-08-08 40 views
6

Info: Utilizzo di Swift e della funzione CGImageSourceCreateWithURL.Swift che accede al dizionario EXIF ​​

Sto tentando di caricare un file da un URL e quindi modificare un dizionario che contiene tutti i dati di quella particolare foto.

Questo è il codice dal file .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) 

Anche se proprietà immagine Stampa tutti i dati come ci si aspetterebbe, nessuno importa quello che ho attmpted estrarre dal dizionario imageProperties - è sempre restituito come null - come ad esempio 'apertura' nell'esempio. L'immagine Proprietà stampa come;

[{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.. 

ho fatto un sacco di ricerca e sperimentazione e io semplicemente non può capire quello che sto facendo male per accedere agli elementi in questo dizionario - Qualcuno potrebbe darmi un esempio di come vorrei impostare una variabile come il Elemento "Modello" all'interno del dizionario?

Grazie per qualsiasi aiuto, John

risposta

6

Per raggiungere i parametri Exif effettuare le seguenti operazioni:

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

C'è un modo per ottenere informazioni sulla fotocamera dalla libreria di foto? Grazie –

+1

Sì, ho modificato la mia risposta per mostrare un esempio su come farlo nell'ultimo Swift/Xcode – Cortex

9

In Swift 3.0 ho trovato la seguente soluzione

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] 

Ora è possibile accedere i tag exif ad esempio

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

Otterrete valori opzionali e dovete testare, se ci sono valori o nullo. Per un elenco delle costanti di proprietà disponibili, consulta lo apple documentation.