Commit 912d880b by Daniel Dahan

updated PhotoLibrary and UIImage extensions

parent f7403019
...@@ -36,7 +36,7 @@ public enum ImageFormat: Int { ...@@ -36,7 +36,7 @@ public enum ImageFormat: Int {
case jpeg case jpeg
} }
public extension UIImage { extension UIImage {
/// Width of the UIImage. /// Width of the UIImage.
public var width: CGFloat { public var width: CGFloat {
return size.width return size.width
...@@ -48,7 +48,7 @@ public extension UIImage { ...@@ -48,7 +48,7 @@ public extension UIImage {
} }
} }
public extension UIImage { extension UIImage {
/** /**
Resizes an image based on a given width. Resizes an image based on a given width.
- Parameter toWidth w: A width value. - Parameter toWidth w: A width value.
...@@ -94,7 +94,7 @@ public extension UIImage { ...@@ -94,7 +94,7 @@ public extension UIImage {
} }
} }
public extension UIImage { extension UIImage {
/** /**
Creates a new image with the passed in color. Creates a new image with the passed in color.
- Parameter color: The UIColor to create the image from. - Parameter color: The UIColor to create the image from.
...@@ -122,7 +122,7 @@ public extension UIImage { ...@@ -122,7 +122,7 @@ public extension UIImage {
} }
} }
public extension UIImage { extension UIImage {
/** /**
Creates an Image that is a color. Creates an Image that is a color.
- Parameter color: The UIColor to create the image from. - Parameter color: The UIColor to create the image from.
...@@ -140,7 +140,7 @@ public extension UIImage { ...@@ -140,7 +140,7 @@ public extension UIImage {
} }
} }
public extension UIImage { extension UIImage {
/** /**
Crops an image to a specified width and height. Crops an image to a specified width and height.
- Parameter toWidth tw: A specified width. - Parameter toWidth tw: A specified width.
...@@ -165,7 +165,7 @@ public extension UIImage { ...@@ -165,7 +165,7 @@ public extension UIImage {
} }
} }
public extension UIImage { extension UIImage {
/** /**
Creates an clear image. Creates an clear image.
- Returns: A UIImage that is clear. - Returns: A UIImage that is clear.
...@@ -178,7 +178,7 @@ public extension UIImage { ...@@ -178,7 +178,7 @@ public extension UIImage {
} }
} }
public extension UIImage { extension UIImage {
/** /**
Asynchronously load images with a completion block. Asynchronously load images with a completion block.
- Parameter URL: A URL destination to fetch the image from. - Parameter URL: A URL destination to fetch the image from.
...@@ -198,7 +198,7 @@ public extension UIImage { ...@@ -198,7 +198,7 @@ public extension UIImage {
} }
} }
public extension UIImage { extension UIImage {
/** /**
Adjusts the orientation of the image from the capture orientation. Adjusts the orientation of the image from the capture orientation.
This is an issue when taking images, the capture orientation is not set correctly This is an issue when taking images, the capture orientation is not set correctly
......
...@@ -232,7 +232,7 @@ public class PhotoLibrary: NSObject { ...@@ -232,7 +232,7 @@ public class PhotoLibrary: NSObject {
enum that describes the response for the authorization request. enum that describes the response for the authorization request.
*/ */
public func requestAuthorization(_ completion: ((PHAuthorizationStatus) -> Void)? = nil) { public func requestAuthorization(_ completion: ((PHAuthorizationStatus) -> Void)? = nil) {
PHPhotoLibrary.requestAuthorization { [weak self] (status) in PHPhotoLibrary.requestAuthorization { [weak self, completion = completion] (status) in
guard let s = self else { guard let s = self else {
return return
} }
...@@ -262,13 +262,12 @@ public class PhotoLibrary: NSObject { ...@@ -262,13 +262,12 @@ public class PhotoLibrary: NSObject {
} }
/** /**
Fetch different PHAssetCollections asynchronously based on different types and subtypes Fetch all the PHAssetCollections asynchronously based on a type and subtype.
with an optional completion block.
- Parameter type: A PHAssetCollectionType. - Parameter type: A PHAssetCollectionType.
- Parameter subtype: A PHAssetCollectionSubtype. - Parameter subtype: A PHAssetCollectionSubtype.
- Parameter completion: An optional completion block. - Parameter completion: A completion block.
*/ */
public func fetch(type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, completion: ([PhotoLibraryDataSource]) -> Void) { public func fetchAssetCollections(with type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, completion: ([PhotoLibraryDataSource]) -> Void) {
self.type = type self.type = type
self.subtype = subtype self.subtype = subtype
...@@ -279,6 +278,9 @@ public class PhotoLibrary: NSObject { ...@@ -279,6 +278,9 @@ public class PhotoLibrary: NSObject {
defer { defer {
DispatchQueue.main.async { [weak self] in DispatchQueue.main.async { [weak self] in
guard let s = self else {
return
}
completion(s.collections) completion(s.collections)
} }
} }
...@@ -302,46 +304,46 @@ public class PhotoLibrary: NSObject { ...@@ -302,46 +304,46 @@ public class PhotoLibrary: NSObject {
options.includeAllBurstAssets = true options.includeAllBurstAssets = true
options.wantsIncrementalChangeDetails = true options.wantsIncrementalChangeDetails = true
var assets = [PHAsset]() s.fetchAssets(in: collection, options: options) { [weak self] (assets, fetchResult) in
let fetchResult = PHAsset.fetchAssets(in: collection, options: options) guard let s = self else {
fetchResult.enumerateObjects(options: []) { (asset, _, _) in return
assets.append(asset) }
s.collections.append(PhotoLibraryDataSource(fetchResult: fetchResult, collection: collection, assets: assets))
} }
s.collections.append(PhotoLibraryDataSource(fetchResult: fetchResult, collection: collection, assets: assets))
} }
} }
} }
/// A method used to prepare the instance object. /**
private func prepare() { Fetch the PHAssets in a given PHAssetCollection.
prepareCollections() - Parameter in assetCollection: A PHAssetCollection.
prepareChangeObservers() - Parameter options: An optional PHFetchOptions object.
} - Parameter completion: A completion block.
*/
/// Prepares the collections. public func fetchAssets(in assetCollection: PHAssetCollection, options: PHFetchOptions?, completion: ([PHAsset], PHFetchResult<PHAsset>) -> Void) {
private func prepareCollections() { var fetchResult: PHFetchResult<PHAsset>!
collections = [PhotoLibraryDataSource]() var assets = [PHAsset]()
defer {
DispatchQueue.main.async { [assets = assets, fetchResult = fetchResult, completion = completion] in
completion(assets, fetchResult!)
}
}
fetchResult = PHAsset.fetchAssets(in: assetCollection, options: options)
fetchResult.enumerateObjects(options: []) { (asset, _, _) in
assets.append(asset)
}
} }
/// A method used to enable change observation. /**
private func prepareChangeObservers() {
PHPhotoLibrary.shared().register(self) */
public func fetchAssets(with mediaType: PHAssetMediaType, options: PHFetchOptions?) -> PHFetchResult<PHAsset> {
return PHAsset.fetchAssets(with: mediaType, options: options)
} }
// public func moments(in momentList: PHCollectionList, options: PHFetchOptions?) -> [PHAssetCollection] {
// var v = [PHAssetCollection]()
// PHAssetCollection.fetchMoments(inMomentList: momentList, options: options).enumerateObjects(options: [.concurrent]) { (collection, _, _) in
// v.append(collection)
// }
// return v
// }
//
// public func fetch(with type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, options: PHFetchOptions?) -> PHFetchResult<PHAssetCollection> {
// let result = PHAssetCollection.fetchAssetCollections(with: type, subtype: subtype, options: options)
// return result
// }
//
/** /**
Performes an asynchronous change to the PHPhotoLibrary database. Performes an asynchronous change to the PHPhotoLibrary database.
- Parameter _ block: A transactional block that ensures that - Parameter _ block: A transactional block that ensures that
...@@ -353,9 +355,28 @@ public class PhotoLibrary: NSObject { ...@@ -353,9 +355,28 @@ public class PhotoLibrary: NSObject {
PHPhotoLibrary.shared().performChanges(block, completionHandler: completion) PHPhotoLibrary.shared().performChanges(block, completionHandler: completion)
} }
/// A method used to prepare the instance object.
private func prepare() {
prepareCollections()
prepareChangeObservers()
}
/// Prepares the collections.
private func prepareCollections() {
collections = [PhotoLibraryDataSource]()
}
/// A method used to enable change observation.
private func prepareChangeObservers() {
PHPhotoLibrary.shared().register(self)
}
}
/// PHImageManager.
extension PhotoLibrary {
/** /**
Retrieves an optional UIImage for a given PHAsset that allows for a targetSize Retrieves an optional UIImage for a given PHAsset that allows for a targetSize
and contentMode. and contentMode.
- Parameter for asset: A PHAsset. - Parameter for asset: A PHAsset.
- Parameter targetSize: A CGSize. - Parameter targetSize: A CGSize.
- Parameter contentMode: A PHImageContentMode. - Parameter contentMode: A PHImageContentMode.
...@@ -363,7 +384,7 @@ public class PhotoLibrary: NSObject { ...@@ -363,7 +384,7 @@ public class PhotoLibrary: NSObject {
- Parameter completion: A completion block. - Parameter completion: A completion block.
- Returns: A PHImageRequestID. - Returns: A PHImageRequestID.
*/ */
public func image(for asset: PHAsset, targetSize: CGSize, contentMode: PHImageContentMode, options: PHImageRequestOptions?, completion: (UIImage?, [NSObject: AnyObject]?) -> Void) -> 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) return PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: contentMode, options: options, resultHandler: completion)
} }
...@@ -374,17 +395,96 @@ public class PhotoLibrary: NSObject { ...@@ -374,17 +395,96 @@ public class PhotoLibrary: NSObject {
- Parameter completion: A completion block. - Parameter completion: A completion block.
- Returns: A PHImageRequestID. - Returns: A PHImageRequestID.
*/ */
public func data(for asset: PHAsset, options: PHImageRequestOptions?, completion: (Data?, String?, UIImageOrientation, [NSObject: AnyObject]?) -> Void) -> 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) return PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: completion)
} }
/** /**
Cance;s an image request for a given PHImageRequestID. Cancels an image request for a given PHImageRequestID.
- Parameter for requestID: A PHImageRequestID. - Parameter for requestID: A PHImageRequestID.
*/ */
public func cancel(for requestID: PHImageRequestID) { public func cancelImageRequest(for requestID: PHImageRequestID) {
PHImageManager.default().cancelImageRequest(requestID) PHImageManager.default().cancelImageRequest(requestID)
} }
/**
*/
@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)
}
/**
*/
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)
}
/**
*/
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)
}
/**
*/
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)
}
}
/// PHCollection.
extension PhotoLibrary {
/**
Fetch PHCollection based on a type and subtype.
- Parameter with type: A PHCollectionListType.
- Parameter subtype: A PHCollectionListSubtype.
- Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion callback.
*/
public func fetchTopLevelUserCollections(with options: PHFetchOptions?, completion: ([PHCollection], PHFetchResult<PHCollection>) -> Void) {
var collections = [PHCollection]()
let fetchResult = PHCollection.fetchTopLevelUserCollections(with: nil)
defer {
DispatchQueue.main.async { [collections = collections, fetchResult = fetchResult, completion = completion] in
completion(collections, fetchResult)
}
}
fetchResult.enumerateObjects(options: [.concurrent]) { (collection, _, _) in
collections.append(collection)
}
}
}
/// PHCollectionList.
extension PhotoLibrary {
/**
Fetch PHCollectionLists based on a type and subtype.
- Parameter with type: A PHCollectionListType.
- Parameter subtype: A PHCollectionListSubtype.
- Parameter options: An optional PHFetchOptions object.
- Parameter completion: A completion callback.
*/
public func fetchCollectionList(with type: PHCollectionListType, subtype: PHCollectionListSubtype, options: PHFetchOptions?, completion: ([PHCollectionList], PHFetchResult<PHCollectionList>) -> Void) {
var lists = [PHCollectionList]()
let fetchResult = PHCollectionList.fetchCollectionLists(with: type, subtype: subtype, options: options)
defer {
DispatchQueue.main.async { [lists = lists, fetchResult = fetchResult, completion = completion] in
completion(lists, fetchResult)
}
}
fetchResult.enumerateObjects(options: [.concurrent]) { (list, _, _) in
lists.append(list)
}
}
} }
/// PHPhotoLibraryChangeObserver extension. /// PHPhotoLibraryChangeObserver extension.
...@@ -409,7 +509,7 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver { ...@@ -409,7 +509,7 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver {
collections.removeAll() collections.removeAll()
fetch(type: t, subtype: st) { [weak self, oldCollections = oldCollections] _ in fetchAssetCollections(with: t, subtype: st) { [weak self, oldCollections = oldCollections] _ in
DispatchQueue.main.async { [weak self, oldCollections = oldCollections] in DispatchQueue.main.async { [weak self, oldCollections = oldCollections] in
guard let s = self else { guard let s = self else {
return return
......
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