处理图像元数据Swift 2.0

时间:2015-09-29 10:22:54

标签: ios swift metadata swift2 ios9

我有一个代码可以在Swift(1.1)中保存图片,从IOS 9开始我尝试将其更改为swift 2.0但是3天后我无法...

所以它是一个简单的函数获取元数据和函数得到这个,更新它并用此创建新的图像。适用于xCode 6.2和Swift(1.1)

所以,如果你能帮助我,请:s

import ImageIO

func savePicture(data: NSData) {
    var img: CGImageRef

    if let source = CGImageSourceCreateWithData(data, nil) {

        var metadata = CGImageSourceCopyPropertiesAtIndex(source, 0, nil)! as Dictionary
        let width = CGFloat(metadata[kCGImagePropertyPixelWidth] as NSNumber)
        let height = CGFloat(metadata[kCGImagePropertyPixelHeight] as NSNumber)
        let reso = width * height


        metadata[kCGImageDestinationLossyCompressionQuality as String] = self.jpegQuality

        var tiff:NSMutableDictionary = metadata[kCGImagePropertyTIFFDictionary] as NSMutableDictionary
        tiff.setValue("Pictures", forKey: kCGImagePropertyTIFFModel)
        var exif:NSMutableDictionary = metadata[kCGImagePropertyExifDictionary] as NSMutableDictionary
        exif.setValue("Pictures", forKey: kCGImagePropertyExifLensModel)

        let gps:NSMutableDictionary = NSMutableDictionary()
        metadata[kCGImagePropertyGPSDictionary] = gps
        gps.setValue(self.loc["lat"], forKey: kCGImagePropertyGPSLatitude)
        gps.setValue(self.loc["lng"], forKey: kCGImagePropertyGPSLongitude)
        self.dateFormatter.dateFormat = "HH:mm:ss"
        gps.setValue(self.dateFormatter.stringFromDate(NSDate()), forKey: kCGImagePropertyGPSTimeStamp)
        self.dateFormatter.dateFormat = "yyyy:MM:dd"
        gps.setValue(self.dateFormatter.stringFromDate(NSDate()), forKey: kCGImagePropertyGPSDateStamp)

        if (reso > self.desiredRes) {
            let ratio = reso / self.desiredRes
            metadata[kCGImageSourceThumbnailMaxPixelSize as String] = max(width, height) / sqrt(ratio)
            metadata[kCGImageSourceCreateThumbnailFromImageIfAbsent as String] = true

            img = CGImageSourceCreateThumbnailAtIndex(source, 0, metadata)

        } else {
            img = CGImageSourceCreateImageAtIndex(source, 0, metadata)
        }


        let uti = "public.jpeg"
        var d = NSMutableData()
        var dest: CGImageDestinationRef = CGImageDestinationCreateWithData(d, uti, 1, nil)
        CGImageDestinationAddImage(dest, img, metadata)
        CGImageDestinationFinalize(dest)

        d.writeToFile(self._makePicName(), atomically: false)


    }

    //        debug purpose: show that metadata have been saved
    //        let d = NSData(contentsOfFile: self.paths.last!)
    //        if let t = CGImageSourceCreateWithData(d, nil) {
    //            let again = CGImageSourceCopyPropertiesAtIndex(t, 0, nil) as Dictionary
    //            println(again)
    //        }

    dispatch_async(dispatch_get_main_queue(), {
        for dlg in self.delegate {
            dlg.didFinishSavingPhoto?()
        }
    })
}

0 个答案:

没有答案