2015-08-08 17 views
6

जानकारी: स्विफ्ट और CGImageSourceCreateWithURL फ़ंक्शन का उपयोग करना।स्विफ्ट accesing EXIF ​​शब्दकोश

मैं एक यूआरएल से एक फ़ाइल लोड करने का प्रयास कर रहा हूं और फिर उस शब्दकोश को संपादित कर रहा हूं जिसमें उस विशेष तस्वीर से सभी डेटा हैं।

यह .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) 

हालांकि छवि गुण सभी डेटा के रूप में उम्मीद होगी प्रिंट, कोई बात नहीं क्या मैं imageProperties शब्दकोश से निकालने के लिए attmpted है - इस तरह के उदाहरण में 'एपर्चर' के रूप में - यह हमेशा अशक्त के रूप में दिया जाता है। छविप्रॉपर्टीज प्रिंट करता है;

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

मैं अनुसंधान और परीक्षण का एक बहुत किया है और मैं बस से बाहर काम नहीं कर सकता मैं इस शब्दकोश में तत्वों का उपयोग करने की गलत क्या कर रही हूं - किसी ने मुझे एक उदाहरण देता हूँ सकता है कि कैसे मैं के रूप में एक चर सेट हैं शब्दकोश के अंदर "मॉडल" तत्व?

किसी भी मदद के लिए धन्यवाद, जॉन

उत्तर

6

निम्न कार्य Exif मानकों तक कैसे पहुंचेंगे:

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

क्या फोटो लाइब्रेरी से कैमरा जानकारी प्राप्त करने का कोई तरीका है? धन्यवाद –

+1

हां, मैंने नवीनतम उत्तर/एक्सकोड में इसे कैसे किया जाए इस पर एक उदाहरण दिखाने के लिए अपना जवाब संपादित किया है – Cortex

9

स्विफ्ट 3.0 में मैं निम्नलिखित समाधान

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] 

अब आप उपयोग कर सकते हैं पाया उदाहरण के लिए exif-टैग

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

आपको वैकल्पिक मान मिलेगा और यदि आपको मूल्य या शून्य हैं तो आपको परीक्षण करना होगा। उपलब्ध संपत्ति स्थिरांक की एक सूची के लिए apple documentation देखें।

संबंधित मुद्दे