-
Notifications
You must be signed in to change notification settings - Fork 56
/
JPEG.swift
executable file
·56 lines (47 loc) · 1.82 KB
/
JPEG.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// JPEG.swift
// LoveLiver
//
// Created by mzp on 10/10/15.
// Copyright © 2015 mzp. All rights reserved.
//
import Foundation
import MobileCoreServices
import ImageIO
class JPEG {
fileprivate let kFigAppleMakerNote_AssetIdentifier = "17"
fileprivate let path : String
init(path : String) {
self.path = path
}
func read() -> String? {
guard let makerNote = metadata()?.object(forKey: kCGImagePropertyMakerAppleDictionary) as! NSDictionary? else {
return nil
}
return makerNote.object(forKey: kFigAppleMakerNote_AssetIdentifier) as! String?
}
func write(_ dest : String, assetIdentifier : String) {
guard let dest = CGImageDestinationCreateWithURL(URL(fileURLWithPath: dest) as CFURL, kUTTypeJPEG, 1, nil)
else { return }
defer { CGImageDestinationFinalize(dest) }
guard let imageSource = self.imageSource() else { return }
guard let metadata = self.metadata()?.mutableCopy() as! NSMutableDictionary! else { return }
let makerNote = NSMutableDictionary()
makerNote.setObject(assetIdentifier, forKey: kFigAppleMakerNote_AssetIdentifier as NSCopying)
metadata.setObject(makerNote, forKey: kCGImagePropertyMakerAppleDictionary as String as String as NSCopying)
CGImageDestinationAddImageFromSource(dest, imageSource, 0, metadata)
}
fileprivate func metadata() -> NSDictionary? {
return self.imageSource().flatMap {
CGImageSourceCopyPropertiesAtIndex($0, 0, nil) as NSDictionary?
}
}
fileprivate func imageSource() -> CGImageSource? {
return self.data().flatMap {
CGImageSourceCreateWithData($0 as CFData, nil)
}
}
fileprivate func data() -> Data? {
return (try? Data(contentsOf: URL(fileURLWithPath: path)))
}
}