-
Notifications
You must be signed in to change notification settings - Fork 3
/
PHPhotoLibrary+PhotoAsset.swift
181 lines (154 loc) · 6.87 KB
/
PHPhotoLibrary+PhotoAsset.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// PHPhotoLibrary+PhotoAsset.swift
//
//
// kudos to ricardopereira
// https://gist.github.com/ricardopereira
//
import Foundation
import Photos
public extension PHPhotoLibrary {
typealias PhotoAsset = PHAsset
typealias PhotoAlbum = PHAssetCollection
static func saveImage(image: UIImage, albumName: String, completion: (PHAsset?)->()) {
if let album = self.findAlbum(albumName) {
saveImage(image, album: album, completion: completion)
return
}
createAlbum(albumName) { album in
if let album = album {
self.saveImage(image, album: album, completion: completion)
}
else {
assert(false, "Album is nil")
}
}
}
static func saveVideo(videoUrl: NSURL, albumName: String, completion: (PHAsset?)->()) {
if let album = self.findAlbum(albumName) {
saveVideo(videoUrl, album: album, completion: completion)
return
}
createAlbum(albumName) { album in
if let album = album {
self.saveVideo(videoUrl, album: album, completion: completion)
}
else {
assert(false, "Album is nil")
}
}
}
static private func saveImage(image: UIImage, album: PhotoAlbum, completion: (PHAsset?)->()) {
var placeholder: PHObjectPlaceholder?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
// Request creating an asset from the image
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
// Request editing the album
guard let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album) else {
assert(false, "Album change request failed")
return
}
// Get a placeholder for the new asset and add it to the album editing request
guard let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else {
assert(false, "Placeholder is nil")
return
}
placeholder = photoPlaceholder
albumChangeRequest.addAssets([photoPlaceholder])
}, completionHandler: { success, error in
guard let placeholder = placeholder else {
assert(false, "Placeholder is nil")
completion(nil)
return
}
if success {
completion(PHAsset.ah_fetchAssetWithLocalIdentifier(placeholder.localIdentifier, options:nil))
}
else {
print(error)
completion(nil)
}
})
}
static private func saveVideo(videoUrl: NSURL, album: PhotoAlbum, completion: (PHAsset?)->()) {
var placeholder: PHObjectPlaceholder?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
// Request creating an asset from the image
let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoUrl)
// Request editing the album
guard let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: album) else {
assert(false, "Album change request failed")
return
}
// Get a placeholder for the new asset and add it to the album editing request
guard let videoPlaceholder = createAssetRequest!.placeholderForCreatedAsset else {
assert(false, "Placeholder is nil")
return
}
placeholder = videoPlaceholder
albumChangeRequest.addAssets([videoPlaceholder])
}, completionHandler: { success, error in
guard let placeholder = placeholder else {
assert(false, "Placeholder is nil")
completion(nil)
return
}
if success {
completion(PHAsset.ah_fetchAssetWithLocalIdentifier(placeholder.localIdentifier, options:nil))
}
else {
print(error)
completion(nil)
}
})
}
static func findAlbum(albumName: String) -> PhotoAlbum? {
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "title = %@", albumName)
let fetchResult = PHAssetCollection.fetchAssetCollectionsWithType(.Album, subtype: .AlbumRegular, options: fetchOptions)
guard let photoAlbum = fetchResult.firstObject as? PHAssetCollection else {
return nil
}
return photoAlbum
}
static func createAlbum(albumName: String, completion: (PhotoAlbum?)->()) {
var albumPlaceholder: PHObjectPlaceholder?
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
// Request creating an album with parameter name
let createAlbumRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollectionWithTitle(albumName)
// Get a placeholder for the new album
albumPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
}, completionHandler: { success, error in
guard let placeholder = albumPlaceholder else {
assert(false, "Album placeholder is nil")
completion(nil)
return
}
let fetchResult = PHAssetCollection.fetchAssetCollectionsWithLocalIdentifiers([placeholder.localIdentifier], options: nil)
guard let album = fetchResult.firstObject as? PhotoAlbum else {
assert(false, "FetchResult has no PHAssetCollection")
completion(nil)
return
}
if success {
completion(album)
}
else {
print(error)
completion(nil)
}
})
}
static func loadThumbnailFromLocalIdentifier(localIdentifier: String, completion: (UIImage?)->()) {
guard let asset = PHAsset.ah_fetchAssetWithLocalIdentifier(localIdentifier, options:nil) else {
completion(nil)
return
}
loadThumbnailFromAsset(asset, completion: completion)
}
static func loadThumbnailFromAsset(asset: PhotoAsset, completion: (UIImage?)->()) {
PHImageManager.defaultManager().requestImageForAsset(asset, targetSize: CGSize(width: 100.0, height: 100.0), contentMode: .AspectFit, options: PHImageRequestOptions(), resultHandler: { result, info in
completion(result)
})
}
}