-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssetLoader.swift
134 lines (100 loc) · 4.63 KB
/
AssetLoader.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
//
// AssetLoader.swift
// videoRenderer
//
// Created by Philip Bernstein on 11/17/16.
// Copyright © 2016 Philip Bernstein. All rights reserved.
//
import Foundation
import Photos
import AVKit
import AVFoundation
/*
Class designed to asyncrhonously load PHAssets from the users photo library, using the Photos framework. While I'd like to use ALAssets, that has been deprecetaed, so we shall work with what we have.
*/
class AssetLoader : NSObject {
var photoAuthorization:Bool? = false
var completionCallback: ((_ success: Bool, _ assets: NSArray) -> ())?
var imageCallback: ((_ success: Bool, _ image: UIImage) -> ())?
var assetCallback: ((_ success: Bool, _ asset: AVAsset, _ audioMix:AVAudioMix) -> ())?
var collectionToUse:PHAssetCollection?
var loadedContent:NSMutableArray?
/*
Checks if we have permission to access photo library, if not, requests access to photo library
*/
func requestPermission() {
if (self.checkPermission() == true) { // we have permission
return // already authorized
}
else { // we need permission, requesting
PHPhotoLibrary.requestAuthorization({ (authorizationStatus) in
var authorized:Bool? = false
let authorizationStatus:PHAuthorizationStatus! = PHPhotoLibrary.authorizationStatus()
if (authorizationStatus == PHAuthorizationStatus.authorized || authorizationStatus == PHAuthorizationStatus.restricted) {
authorized = true
}
self.photoAuthorization = authorized
if (authorized)! { // now that we are permitted, load a list of PHAssets that we can use
self.loadAssets()
}
})
}
}
/*
Simply checks current photo library authorization status
*/
func checkPermission()->Bool! {
var authorized:Bool? = false
let authorizationStatus:PHAuthorizationStatus! = PHPhotoLibrary.authorizationStatus()
if (authorizationStatus == PHAuthorizationStatus.authorized || authorizationStatus == PHAuthorizationStatus.restricted) {
authorized = true
}
self.photoAuthorization = authorized
return authorized;
}
/*
Loads assets using Photos library, returned as PHAssets, sorted by date taken
*/
func loadAssets() {
// no permission, we return an empty array as our callback
if (self.photoAuthorization == false && self.completionCallback != nil) {
self.completionCallback!(false, [])
return
}
// we want to sort by creation date, newest first
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
let collection:PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
var assetCollection:PHAssetCollection?
assetCollection = collection.firstObject;
self.collectionToUse = assetCollection;
let photoAssets = PHAsset.fetchAssets(in: assetCollection!, options: nil)
let content:NSMutableArray = NSMutableArray()
photoAssets.enumerateObjects({ (asset, index, unsafe) in
content.add(asset)
})
// end result is array of assets that we can use
self.loadedContent = content
if ((self.completionCallback) != nil) {
self.completionCallback!(true, self.loadedContent!)
}
}
func loadImageForAsset(asset: PHAsset) {
let options = PHImageRequestOptions()
options.resizeMode = .exact
PHImageManager.default().requestImage(for: asset, targetSize: CGSize.init(width: 1024, height: 1024), contentMode: .aspectFill, options: options, resultHandler: {(result, info)in
if (self.imageCallback != nil) {
self.imageCallback!(true, result!)
}
})
}
func loadAssetForVideo(asset: PHAsset) {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: asset, options: options, resultHandler: {(resultAsset: AVAsset?, audioMix: AVAudioMix?, info: [AnyHashable : Any]?) -> Void in
if (self.assetCallback != nil) {
self.assetCallback!(true, resultAsset!,audioMix!)
}
})
}
}