-
Notifications
You must be signed in to change notification settings - Fork 0
/
LivePhotoImageResourceWriter.swift
executable file
·56 lines (47 loc) · 1.85 KB
/
LivePhotoImageResourceWriter.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
//
// LivePhotoImageResourceWriter.swift
// Live Photos
//
// Originally Created by genadyo (github.com/genadyo).
// Newly Written by metasmile (github.com/metasmile) on 9/12/16.
//
import Foundation
import MobileCoreServices
import ImageIO
public class LivePhotoImageResourceWriter: NSObject {
private let kFigAppleMakerNote_AssetIdentifier = "17"
public var path : String
public init(path : String) {
self.path = path
}
func read() -> String? {
guard let makerNote = metadata()?.objectForKey(kCGImagePropertyMakerAppleDictionary) as! NSDictionary? else {
return nil
}
return makerNote.objectForKey(kFigAppleMakerNote_AssetIdentifier) as! String?
}
public func write(destPath: String, assetIdentifier : String) {
guard let dest = CGImageDestinationCreateWithURL(NSURL(fileURLWithPath: destPath), 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)
metadata.setObject(makerNote, forKey: kCGImagePropertyMakerAppleDictionary as String)
CGImageDestinationAddImageFromSource(dest, imageSource, 0, metadata)
}
private func metadata() -> NSDictionary? {
return self.imageSource().flatMap {
CGImageSourceCopyPropertiesAtIndex($0, 0, nil) as NSDictionary?
}
}
private func imageSource() -> CGImageSourceRef? {
return self.data().flatMap {
CGImageSourceCreateWithData($0, nil)
}
}
private func data() -> NSData? {
return NSData(contentsOfFile: path)
}
}