我需要使用swift 3.0將我的UIImage轉換爲漸進式jpeg。 我發現在迅速2.2以下代碼:在swift 3.0中將圖像轉換爲逐行JPEG
let sourceImage = UIImage(named: "example.jpg")
let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString).stringByAppendingPathComponent("progressive.jpg")
let fileUrl = NSURL(fileURLWithPath: path as String, isDirectory: true)
let url = CFURLCreateWithString(kCFAllocatorDefault,fileUrl.absoluteString as CFString , nil)
let destinationRef = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, nil)
let jfifProperties = NSDictionary(dictionary: [kCGImagePropertyJFIFIsProgressive:kCFBooleanTrue])
let properties = NSDictionary(dictionary: [kCGImageDestinationLossyCompressionQuality:0.6,kCGImagePropertyJFIFDictionary:jfifProperties])
CGImageDestinationAddImage(destinationRef!, (sourceImage?.CGImage)!, properties)
CGImageDestinationFinalize(destinationRef!)
不過,這並不在SWIFT 3.0。 CGImageDestinationCreateWithURL給出錯誤(所有的CGImage類...)有什麼幫助?謝謝!
「我發現下面的代碼」。不要使用你不明白的代碼。 – Sulthan
謝謝你的幫助。對此,我真的非常感激! –