Commit 09db164d by Daniel Dahan

added PhotoLibrary support

parent 10d035e7
...@@ -30,17 +30,6 @@ ...@@ -30,17 +30,6 @@
import Photos import Photos
public struct PhotoLibraryDataSource {
/// A reference to the calling PHFetchResult.
public private(set) var fetchResult: PHFetchResult<PHAsset>
/// A reference to a PHAssetCollection returned from the fetchResult.
public private(set) var collection: PHAssetCollection
/// A reference to an Array of PHAssets for the PHAssetCollection.
public private(set) var assets: [PHAsset]
}
@objc(PhotoLibraryMove) @objc(PhotoLibraryMove)
public class PhotoLibraryMove: NSObject { public class PhotoLibraryMove: NSObject {
/// An index that is being moved from. /// An index that is being moved from.
...@@ -61,8 +50,11 @@ public class PhotoLibraryMove: NSObject { ...@@ -61,8 +50,11 @@ public class PhotoLibraryMove: NSObject {
} }
public struct PhotoLibraryFetchResultDataSource { public struct PhotoLibraryFetchResultDataSource {
public private(set) var fetchResult: PHFetchResult<PHObject> /// A reference to the PHFetchResults.
public private(set) var objects: [PHObject] public internal(set) var fetchResult: PHFetchResult<PHObject>
/// A reference to the objects associated with the PHFetchResult.
public internal(set) var objects: [PHObject]
} }
@objc(PhotoLibraryDelegate) @objc(PhotoLibraryDelegate)
...@@ -181,28 +173,7 @@ public class PhotoLibrary: NSObject { ...@@ -181,28 +173,7 @@ public class PhotoLibrary: NSObject {
public private(set) lazy var cachingImageManager = PHCachingImageManager() public private(set) lazy var cachingImageManager = PHCachingImageManager()
/// A reference to all current PHFetchResults. /// A reference to all current PHFetchResults.
public private(set) lazy var fetchResults = [PhotoLibraryFetchResultDataSource]() public private(set) lazy var fetchResultsDataSource = [String: PhotoLibraryFetchResultDataSource]()
/// The assets used in the album.
public private(set) var collections = [PhotoLibraryDataSource]() {
willSet {
guard .authorized == authorizationStatus else {
return
}
cachingImageManager.stopCachingImagesForAllAssets()
}
didSet {
guard .authorized == authorizationStatus else {
return
}
for dataSource in collections {
cachingImageManager.startCachingImages(for: dataSource.assets, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: nil)
}
}
}
/// A reference to a PhotoLibraryDelegate. /// A reference to a PhotoLibraryDelegate.
public weak var delegate: PhotoLibraryDelegate? public weak var delegate: PhotoLibraryDelegate?
...@@ -223,70 +194,6 @@ public class PhotoLibrary: NSObject { ...@@ -223,70 +194,6 @@ public class PhotoLibrary: NSObject {
prepare() prepare()
} }
// /**
// Fetch all the PHAssetCollections asynchronously based on a type and subtype.
// - Parameter type: A PHAssetCollectionType.
// - Parameter subtype: A PHAssetCollectionSubtype.
// - Parameter completion: A completion block.
// */
// public func fetchAssetCollections(with type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, completion: ([PhotoLibraryDataSource]) -> Void) {
// self.type = type
// self.subtype = subtype
//
// DispatchQueue.global(qos: .default).async { [weak self, type = type, subtype = subtype, completion = completion] in
// guard let s = self else {
// return
// }
//
// defer {
// DispatchQueue.main.async { [weak self] in
// guard let s = self else {
// return
// }
// completion(s.collections)
// }
// }
//
// let options = PHFetchOptions()
// options.includeHiddenAssets = true
// options.includeAllBurstAssets = true
// options.wantsIncrementalChangeDetails = true
//
// s.collectionFetchResult = PHAssetCollection.fetchAssetCollections(with: type, subtype: subtype, options: options)
//
// s.collectionFetchResult?.enumerateObjects(options: [.concurrent]) { [weak self] (collection, _, _) in
// guard let s = self else {
// return
// }
//
// let options = PHFetchOptions()
// let descriptor = NSSortDescriptor(key: "creationDate", ascending: false)
// options.sortDescriptors = [descriptor]
// options.includeHiddenAssets = true
// options.includeAllBurstAssets = true
// options.wantsIncrementalChangeDetails = true
//
// s.fetchAssets(in: collection, options: options) { [weak self] (assets, fetchResult) in
// guard let s = self else {
// return
// }
// s.collections.append(PhotoLibraryDataSource(fetchResult: fetchResult, collection: collection, assets: assets))
// }
// }
// }
// }
/**
Performes an asynchronous change to the PHPhotoLibrary database.
- Parameter _ block: A transactional block that ensures that
all changes to the PHPhotoLibrary are atomic.
- Parameter completion: A completion block that is executed once the
transaction has been completed.
*/
public func performChanges(_ block: () -> Void, completion: ((Bool, Error?) -> Void)? = nil) {
PHPhotoLibrary.shared().performChanges(block, completionHandler: completion)
}
/// A method used to prepare the instance object. /// A method used to prepare the instance object.
private func prepare() { private func prepare() {
prepareChangeObservers() prepareChangeObservers()
...@@ -338,95 +245,6 @@ extension PhotoLibrary { ...@@ -338,95 +245,6 @@ extension PhotoLibrary {
} }
} }
/// PHImageManager.
extension PhotoLibrary {
/**
Retrieves an optional UIImage for a given PHAsset that allows for a targetSize
and contentMode.
- Parameter for asset: A PHAsset.
- Parameter targetSize: A CGSize.
- Parameter contentMode: A PHImageContentMode.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestImage(for asset: PHAsset, targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?, completion: (UIImage?, [NSObject: AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: contentMode, options: options, resultHandler: completion)
}
/**
Retrieves an optional Data object for a given PHAsset.
- Parameter for asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestImageData(for asset: PHAsset, options: PHImageRequestOptions?, completion: (Data?, String?, UIImageOrientation, [NSObject: AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: completion)
}
/**
Cancels an image request for a given PHImageRequestID.
- Parameter for requestID: A PHImageRequestID.
*/
public func cancelImageRequest(for requestID: PHImageRequestID) {
PHImageManager.default().cancelImageRequest(requestID)
}
/**
Requests a live photo representation of the asset. With
oportunistic (or if no
options are specified), the resultHandler block may be
called more than once (the first call may occur before
the method returns). The PHImageResultIsDegradedKey key
in the result handler's info parameter indicates when a
temporary low-quality live photo is provided.
- Parameter for asset: A PHAsset.
- Parameter targetSize: A CGSize.
- Parameter contentMode: A PHImageContentMode.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
@available(iOS 9.1, *)
public func requestLivePhoto(for asset: PHAsset, targetSize: CGSize, contentMode: PHImageContentMode, options: PHLivePhotoRequestOptions?, completion: (PHLivePhoto?, [NSObject : AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestLivePhoto(for: asset, targetSize: targetSize, contentMode: contentMode, options: options, resultHandler: completion)
}
/**
For playback only.
- Parameter forVideo asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestPlayerItem(forVideo asset: PHAsset, options: PHVideoRequestOptions?, completion: (AVPlayerItem?, [NSObject : AnyObject]?) -> Swift.Void) -> PHImageRequestID {
return PHImageManager.default().requestPlayerItem(forVideo: asset, options: options, resultHandler: completion)
}
/**
Export.
- Parameter forVideo asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestExportSession(forVideo asset: PHAsset, options: PHVideoRequestOptions?, exportPreset: String, completion: (AVAssetExportSession?, [NSObject : AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: exportPreset, resultHandler: completion)
}
/**
For all other requests.
- Parameter forVideo asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestAVAsset(forVideo asset: PHAsset, options: PHVideoRequestOptions?, completion: (AVAsset?, AVAudioMix?, [NSObject : AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestAVAsset(forVideo: asset, options: options, resultHandler: completion)
}
}
/// Fetch. /// Fetch.
extension PhotoLibrary { extension PhotoLibrary {
/** /**
...@@ -434,22 +252,24 @@ extension PhotoLibrary { ...@@ -434,22 +252,24 @@ extension PhotoLibrary {
- Parameter fetchResult: A PHFetchResult<T>. - Parameter fetchResult: A PHFetchResult<T>.
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
private func fetch<T: PHFetchResult<U>, U: PHObject>(result: T, completion: ([U], T) -> Void) { private func fetch<T: PHFetchResult<U>, U: PHObject>(caller: String, result: T, completion: ([U], T) -> Void) {
var objects = [U]() var objects = [U]()
defer {
// Stores for change observation.
fetchResults.append(PhotoLibraryFetchResultDataSource(fetchResult: result as! PHFetchResult<PHObject>, objects: objects))
}
result.enumerateObjects({ (collection, _, _) in result.enumerateObjects({ (collection, _, _) in
objects.append(collection) objects.append(collection)
}) })
// Used in change observation.
fetchResultsDataSource[caller] = PhotoLibraryFetchResultDataSource(fetchResult: result as! PHFetchResult<PHObject>, objects: objects)
if Thread.isMainThread {
completion(objects, result)
} else {
DispatchQueue.main.async { [objects = objects, result = result, completion = completion] in DispatchQueue.main.async { [objects = objects, result = result, completion = completion] in
completion(objects, result) completion(objects, result)
} }
} }
}
} }
/// PHCollectionList. /// PHCollectionList.
...@@ -464,7 +284,7 @@ extension PhotoLibrary { ...@@ -464,7 +284,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollectionListsContaining(_ collection: PHCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionListsContaining(_ collection: PHCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
fetch(result: PHCollectionList.fetchCollectionListsContaining(collection, options: options), completion: completion) fetch(caller: #function, result: PHCollectionList.fetchCollectionListsContaining(collection, options: options), completion: completion)
} }
/** /**
...@@ -475,7 +295,7 @@ extension PhotoLibrary { ...@@ -475,7 +295,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollectionList(with type: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionList(with type: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
fetch(result: PHCollectionList.fetchCollectionLists(with: type, subtype: subtype, options: options), completion: completion) fetch(caller: #function, result: PHCollectionList.fetchCollectionLists(with: type, subtype: subtype, options: options), completion: completion)
} }
/** /**
...@@ -488,7 +308,7 @@ extension PhotoLibrary { ...@@ -488,7 +308,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollectionLists(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionLists(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
fetch(result: PHCollectionList.fetchCollectionLists(withLocalIdentifiers: identifiers, options: options), completion: completion) fetch(caller: #function, result: PHCollectionList.fetchCollectionLists(withLocalIdentifiers: identifiers, options: options), completion: completion)
} }
/** /**
...@@ -501,7 +321,7 @@ extension PhotoLibrary { ...@@ -501,7 +321,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollectionLists(with collectionListType: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchCollectionLists(with collectionListType: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
fetch(result: PHCollectionList.fetchCollectionLists(with: collectionListType, subtype: subtype, options: options), completion: completion) fetch(caller: #function, result: PHCollectionList.fetchCollectionLists(with: collectionListType, subtype: subtype, options: options), completion: completion)
} }
/** /**
...@@ -512,7 +332,7 @@ extension PhotoLibrary { ...@@ -512,7 +332,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, containingMoment moment: PHAssetCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, containingMoment moment: PHAssetCollection, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
fetch(result: PHCollectionList.fetchMomentLists(with: momentListSubtype, containingMoment: moment, options: options), completion: completion) fetch(caller: #function, result: PHCollectionList.fetchMomentLists(with: momentListSubtype, containingMoment: moment, options: options), completion: completion)
} }
/** /**
...@@ -522,7 +342,7 @@ extension PhotoLibrary { ...@@ -522,7 +342,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) { public func fetchMomentLists(with momentListSubtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
fetch(result: PHCollectionList.fetchMomentLists(with: momentListSubtype, options: options), completion: completion) fetch(caller: #function, result: PHCollectionList.fetchMomentLists(with: momentListSubtype, options: options), completion: completion)
} }
} }
...@@ -535,7 +355,7 @@ extension PhotoLibrary { ...@@ -535,7 +355,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchCollections(in collectionList: PHCollectionList, options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) { public func fetchCollections(in collectionList: PHCollectionList, options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) {
fetch(result: PHCollection.fetchCollections(in: collectionList, options: options), completion: completion) fetch(caller: #function, result: PHCollection.fetchCollections(in: collectionList, options: options), completion: completion)
} }
/** /**
...@@ -546,7 +366,7 @@ extension PhotoLibrary { ...@@ -546,7 +366,7 @@ extension PhotoLibrary {
- Parameter completion: A completion callback. - Parameter completion: A completion callback.
*/ */
public func fetchTopLevelUserCollections(with options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) { public func fetchTopLevelUserCollections(with options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) {
fetch(result: PHCollection.fetchTopLevelUserCollections(with: nil), completion: completion) fetch(caller: #function, result: PHCollection.fetchTopLevelUserCollections(with: nil), completion: completion)
} }
} }
...@@ -560,7 +380,7 @@ extension PhotoLibrary { ...@@ -560,7 +380,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssetCollections(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) { public func fetchAssetCollections(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) {
fetch(result: PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: identifiers, options: options), completion: completion) fetch(caller: #function, result: PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: identifiers, options: options), completion: completion)
} }
/** /**
...@@ -572,7 +392,7 @@ extension PhotoLibrary { ...@@ -572,7 +392,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssetCollections(with type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) { public func fetchAssetCollections(with type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) {
fetch(result: PHAssetCollection.fetchAssetCollections(with: type, subtype: subtype, options: options), completion: completion) fetch(caller: #function, result: PHAssetCollection.fetchAssetCollections(with: type, subtype: subtype, options: options), completion: completion)
} }
/** /**
...@@ -583,7 +403,7 @@ extension PhotoLibrary { ...@@ -583,7 +403,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssetCollectionsContaining(_ asset: PHAsset, with type: PHAssetCollectionType, options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) { public func fetchAssetCollectionsContaining(_ asset: PHAsset, with type: PHAssetCollectionType, options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) {
fetch(result: PHAssetCollection.fetchAssetCollectionsContaining(asset, with: type, options: options), completion: completion) fetch(caller: #function, result: PHAssetCollection.fetchAssetCollectionsContaining(asset, with: type, options: options), completion: completion)
} }
/** /**
...@@ -595,7 +415,7 @@ extension PhotoLibrary { ...@@ -595,7 +415,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssetCollections(withALAssetGroupURLs assetGroupURLs: [URL], options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) { public func fetchAssetCollections(withALAssetGroupURLs assetGroupURLs: [URL], options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) {
fetch(result: PHAssetCollection.fetchAssetCollections(withALAssetGroupURLs: assetGroupURLs, options: options), completion: completion) fetch(caller: #function, result: PHAssetCollection.fetchAssetCollections(withALAssetGroupURLs: assetGroupURLs, options: options), completion: completion)
} }
/** /**
...@@ -605,7 +425,7 @@ extension PhotoLibrary { ...@@ -605,7 +425,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchMoments(inMomentList momentList: PHCollectionList, options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) { public func fetchMoments(inMomentList momentList: PHCollectionList, options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) {
fetch(result: PHAssetCollection.fetchMoments(inMomentList: momentList, options: options), completion: completion) fetch(caller: #function, result: PHAssetCollection.fetchMoments(inMomentList: momentList, options: options), completion: completion)
} }
/** /**
...@@ -614,7 +434,7 @@ extension PhotoLibrary { ...@@ -614,7 +434,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchMoments(with options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) { public func fetchMoments(with options: PHFetchOptions?, completion: ([PHAssetCollection], PHFetchResult<PHAssetCollection>) -> Void) {
fetch(result: PHAssetCollection.fetchMoments(with: options), completion: completion) fetch(caller: #function, result: PHAssetCollection.fetchMoments(with: options), completion: completion)
} }
} }
...@@ -627,7 +447,7 @@ extension PhotoLibrary { ...@@ -627,7 +447,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetch(result: PHAsset.fetchAssets(in: assetCollection, options: options), completion: completion) fetch(caller: #function, result: PHAsset.fetchAssets(in: assetCollection, options: options), completion: completion)
} }
/** /**
...@@ -638,7 +458,7 @@ extension PhotoLibrary { ...@@ -638,7 +458,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(withLocalIdentifiers identifiers: [String], options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetch(result: PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: options), completion: completion) fetch(caller: #function, result: PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: options), completion: completion)
} }
/** /**
...@@ -652,7 +472,7 @@ extension PhotoLibrary { ...@@ -652,7 +472,7 @@ extension PhotoLibrary {
guard let fetchResult = PHAsset.fetchKeyAssets(in: assetCollection, options: options) else { guard let fetchResult = PHAsset.fetchKeyAssets(in: assetCollection, options: options) else {
return nil return nil
} }
fetch(result: fetchResult, completion: completion) fetch(caller: #function, result: fetchResult, completion: completion)
return fetchResult return fetchResult
} }
...@@ -664,7 +484,7 @@ extension PhotoLibrary { ...@@ -664,7 +484,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(withBurstIdentifier burstIdentifier: String, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(withBurstIdentifier burstIdentifier: String, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetch(result: PHAsset.fetchAssets(withBurstIdentifier: burstIdentifier, options: options), completion: completion) fetch(caller: #function, result: PHAsset.fetchAssets(withBurstIdentifier: burstIdentifier, options: options), completion: completion)
} }
/** /**
...@@ -674,7 +494,7 @@ extension PhotoLibrary { ...@@ -674,7 +494,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(with options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(with options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetch(result: PHAsset.fetchAssets(with: options), completion: completion) fetch(caller: #function, result: PHAsset.fetchAssets(with: options), completion: completion)
} }
/** /**
...@@ -684,7 +504,7 @@ extension PhotoLibrary { ...@@ -684,7 +504,7 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(with mediaType: PHAssetMediaType, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(with mediaType: PHAssetMediaType, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetch(result: PHAsset.fetchAssets(with: mediaType, options: options), completion: completion) fetch(caller: #function, result: PHAsset.fetchAssets(with: mediaType, options: options), completion: completion)
} }
/** /**
...@@ -695,7 +515,96 @@ extension PhotoLibrary { ...@@ -695,7 +515,96 @@ extension PhotoLibrary {
- Parameter completion: A completion block. - Parameter completion: A completion block.
*/ */
public func fetchAssets(withALAssetURLs assetURLs: [URL], options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) { public func fetchAssets(withALAssetURLs assetURLs: [URL], options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
fetch(result: PHAsset.fetchAssets(withALAssetURLs: assetURLs, options: options), completion: completion) fetch(caller: #function, result: PHAsset.fetchAssets(withALAssetURLs: assetURLs, options: options), completion: completion)
}
}
/// PHImageManager.
extension PhotoLibrary {
/**
Retrieves an optional UIImage for a given PHAsset that allows for a targetSize
and contentMode.
- Parameter for asset: A PHAsset.
- Parameter targetSize: A CGSize.
- Parameter contentMode: A PHImageContentMode.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestImage(for asset: PHAsset, targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?, completion: (UIImage?, [NSObject: AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: contentMode, options: options, resultHandler: completion)
}
/**
Retrieves an optional Data object for a given PHAsset.
- Parameter for asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestImageData(for asset: PHAsset, options: PHImageRequestOptions?, completion: (Data?, String?, UIImageOrientation, [NSObject: AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: completion)
}
/**
Cancels an image request for a given PHImageRequestID.
- Parameter for requestID: A PHImageRequestID.
*/
public func cancelImageRequest(for requestID: PHImageRequestID) {
PHImageManager.default().cancelImageRequest(requestID)
}
/**
Requests a live photo representation of the asset. With
oportunistic (or if no
options are specified), the resultHandler block may be
called more than once (the first call may occur before
the method returns). The PHImageResultIsDegradedKey key
in the result handler's info parameter indicates when a
temporary low-quality live photo is provided.
- Parameter for asset: A PHAsset.
- Parameter targetSize: A CGSize.
- Parameter contentMode: A PHImageContentMode.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
@available(iOS 9.1, *)
public func requestLivePhoto(for asset: PHAsset, targetSize: CGSize, contentMode: PHImageContentMode, options: PHLivePhotoRequestOptions?, completion: (PHLivePhoto?, [NSObject : AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestLivePhoto(for: asset, targetSize: targetSize, contentMode: contentMode, options: options, resultHandler: completion)
}
/**
For playback only.
- Parameter forVideo asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestPlayerItem(forVideo asset: PHAsset, options: PHVideoRequestOptions?, completion: (AVPlayerItem?, [NSObject : AnyObject]?) -> Swift.Void) -> PHImageRequestID {
return PHImageManager.default().requestPlayerItem(forVideo: asset, options: options, resultHandler: completion)
}
/**
Export.
- Parameter forVideo asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestExportSession(forVideo asset: PHAsset, options: PHVideoRequestOptions?, exportPreset: String, completion: (AVAssetExportSession?, [NSObject : AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestExportSession(forVideo: asset, options: options, exportPreset: exportPreset, resultHandler: completion)
}
/**
For all other requests.
- Parameter forVideo asset: A PHAsset.
- Parameter options: A PHImageRequestOptions.
- Parameter completion: A completion block.
- Returns: A PHImageRequestID.
*/
public func requestAVAsset(forVideo asset: PHAsset, options: PHVideoRequestOptions?, completion: (AVAsset?, AVAudioMix?, [NSObject : AnyObject]?) -> Void) -> PHImageRequestID {
return PHImageManager.default().requestAVAsset(forVideo: asset, options: options, resultHandler: completion)
} }
} }
...@@ -707,74 +616,80 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver { ...@@ -707,74 +616,80 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver {
photo library. photo library.
*/ */
public func photoLibraryDidChange(_ changeInfo: PHChange) { public func photoLibraryDidChange(_ changeInfo: PHChange) {
for dataSource in fetchResults { DispatchQueue.main.async { [weak self, changeInfo = changeInfo] in
print(dataSource) guard let s = self else {
} return
}
// fetchAssetCollections(with: t, subtype: st) { [weak self, oldCollections = oldCollections] _ in
// DispatchQueue.main.async { [weak self, oldCollections = oldCollections] in s.delegate?.photoLibrary?(photoLibrary: s, didChange: changeInfo)
// guard let s = self else {
// return for (_, var dataSource) in s.fetchResultsDataSource {
// } for i in 0..<dataSource.objects.count {
// let object = dataSource.objects[i]
// // Notify about the general change.
// s.delegate?.photoLibrary?(photoLibrary: s, didChange: changeInfo) guard let details = changeInfo.changeDetails(for: object) else {
// continue
// // Notifiy about specific changes. }
// s.collectionFetchResult?.enumerateObjects(options: .concurrent) { [weak self, changeInfo = changeInfo] (collection, _, _) in
// guard let s = self else { guard let afterChanges = details.objectAfterChanges else {
// return continue
// } }
//
// guard let details = changeInfo.changeDetails(for: collection) else { dataSource.objects[i] = afterChanges
// return
// } s.delegate?.photoLibrary?(photoLibrary: s, beforeChanges: details.objectBeforeChanges, afterChanges: afterChanges, assetContentChanged: details.assetContentChanged, objectWasDeleted: details.objectWasDeleted)
// }
// guard let afterChanges = details.objectAfterChanges else {
// return if let details = changeInfo.changeDetails(for: dataSource.fetchResult as! PHFetchResult<AnyObject>) {
// } s.delegate?.photoLibrary?(photoLibrary: s, fetchBeforeChanges: details.fetchResultBeforeChanges, fetchAfterChanges: details.fetchResultAfterChanges)
//
// s.delegate?.photoLibrary?(photoLibrary: s, beforeChanges: details.objectBeforeChanges, afterChanges: afterChanges, assetContentChanged: details.assetContentChanged, objectWasDeleted: details.objectWasDeleted) dataSource.fetchResult = details.fetchResultAfterChanges
// }
// guard details.hasIncrementalChanges else {
// for i in 0..<oldCollections.count { continue
// let dataSource = oldCollections[i] }
//
// if let details = changeInfo.changeDetails(for: dataSource.fetchResult as! PHFetchResult<AnyObject>) { let removedIndexes = details.removedIndexes
// s.delegate?.photoLibrary?(photoLibrary: s, fetchBeforeChanges: details.fetchResultBeforeChanges, fetchAfterChanges: details.fetchResultAfterChanges) let insertedIndexes = details.insertedIndexes
// let changedIndexes = details.changedIndexes
// guard details.hasIncrementalChanges else {
// return if nil != removedIndexes {
// } s.delegate?.photoLibrary?(photoLibrary: s, removed: removedIndexes!, for: details.removedObjects)
// }
// let removedIndexes = details.removedIndexes
// let insertedIndexes = details.insertedIndexes if nil != insertedIndexes {
// let changedIndexes = details.changedIndexes s.delegate?.photoLibrary?(photoLibrary: s, inserted: insertedIndexes!, for: details.insertedObjects)
// }
// if nil != removedIndexes {
// s.delegate?.photoLibrary?(photoLibrary: s, removed: removedIndexes!, for: details.removedObjects) if nil != changedIndexes {
// } s.delegate?.photoLibrary?(photoLibrary: s, changed: changedIndexes!, for: details.changedObjects)
// }
// if nil != insertedIndexes {
// s.delegate?.photoLibrary?(photoLibrary: s, inserted: insertedIndexes!, for: details.insertedObjects) var moves = [PhotoLibraryMove]()
// }
// if details.hasMoves {
// if nil != changedIndexes { details.enumerateMoves { (from, to) in
// s.delegate?.photoLibrary?(photoLibrary: s, changed: changedIndexes!, for: details.changedObjects) moves.append(PhotoLibraryMove(from: from, to: to))
// } }
// }
// var moves = [PhotoLibraryMove]()
// s.delegate?.photoLibrary?(photoLibrary: s, removedIndexes: removedIndexes, insertedIndexes: insertedIndexes, changedIndexes: changedIndexes, has: moves)
// if details.hasMoves { }
// details.enumerateMoves { (from, to) in }
// moves.append(PhotoLibraryMove(from: from, to: to)) }
// } }
// } }
//
// s.delegate?.photoLibrary?(photoLibrary: s, removedIndexes: removedIndexes, insertedIndexes: insertedIndexes, changedIndexes: changedIndexes, has: moves) /// Requesting changes.
// } extension PhotoLibrary {
// } /**
// } Performes an asynchronous change to the PHPhotoLibrary database.
// } - Parameter _ block: A transactional block that ensures that
all changes to the PHPhotoLibrary are atomic.
- Parameter completion: A completion block that is executed once the
transaction has been completed.
*/
public func performChanges(_ block: () -> Void, completion: ((Bool, Error?) -> Void)? = nil) {
PHPhotoLibrary.shared().performChanges(block, completionHandler: completion)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment