Skip to content

Commit

Permalink
Merge pull request #1 from cristinaITdeveloper/cristinaITdeveloper-ne…
Browse files Browse the repository at this point in the history
…w-methods

Cristina developer - New methods
  • Loading branch information
SagarSDagdu authored Jan 4, 2018
2 parents 9c14f66 + d36007c commit baa7a16
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion SDPhotosHelper/Classes/SDPhotosHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public class SDPhotosHelper: NSObject {
static let assetCreationFailure = NSError(domain: "SDPhotosHelper", code: 1002, userInfo: [NSLocalizedDescriptionKey : "Asset could not be added from the given source"])
static let albumNotFoundError : NSError = NSError(domain: "SDPhotosHelper", code: 1003, userInfo: [NSLocalizedDescriptionKey : "Album with given name was not found"])


//MARK:- Methods

public static func createAlbum(withTitle title:String,
Expand Down Expand Up @@ -116,6 +115,65 @@ public class SDPhotosHelper: NSObject {
}
}

public static func getImages(fromAlbum album:String,
onSuccess success:@escaping([UIImage])->Void,
onFailure failure:@escaping(Error?)->Void) {

guard let album = self.getAlbum(withName: album) else {
failure(SDPhotosHelper.albumNotFoundError)
return
}
let optionsOrderImage = PHFetchOptions()
optionsOrderImage.sortDescriptors = [ NSSortDescriptor(key: "creationDate", ascending: false) ]

let photos = PHAsset.fetchAssets(in: album, options: optionsOrderImage)
var images : [UIImage] = []

photos.enumerateObjects(_:) { (asset, count, stop) in
let image = SDPhotosHelper.getImageFromAsset(asset)
images.append(image)
}
OperationQueue.main.addOperation({
success(images)
})
}

public static func deleteImage(withIdentifier identifier:String,
fromAlbum album:String,
onSuccess success:@escaping(Bool)->Void,
onFailure failure:@escaping(Error?)->Void) {
var deleted = false
if let fetchresult = self.getAssetFetchResult(fromAlbum: album, withLocalIdentifier: identifier) {

PHPhotoLibrary.shared().performChanges({
if let album = getAlbum(withName: album) {
let request = PHAssetCollectionChangeRequest(for: album, assets: fetchresult)
if let asset = fetchresult.firstObject {
let fastEnumeration : NSArray = [asset] as NSArray
request?.removeAssets(fastEnumeration)
deleted = true
}
else {
failure(SDPhotosHelper.assetNotFoundError)
}
}
else {
failure(SDPhotosHelper.albumNotFoundError)
}
}, completionHandler: { (didSucceed, error) in

OperationQueue.main.addOperation({
didSucceed ? success(deleted) : failure(error)
})

})
} else {
OperationQueue.main.addOperation({
failure(SDPhotosHelper.assetNotFoundError)
})
}
}

//MARK:- Video Utilities

public static func addNewVideo(withFileUrl fileUrl:URL,
Expand Down Expand Up @@ -176,6 +234,19 @@ public class SDPhotosHelper: NSObject {

//MARK:- Private helper methods

fileprivate static func getImageFromAsset(_ asset: PHAsset) -> UIImage {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var image = UIImage()
option.isSynchronous = true

manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: option, resultHandler: {(result, info)->Void in
image = result!
})
return image
}


fileprivate static func getAsset(fromAlbum album:String,
withLocalIdentifier localIdentifier:String) -> PHAsset? {

Expand All @@ -192,6 +263,17 @@ public class SDPhotosHelper: NSObject {
return fetchResult.firstObject
}

fileprivate static func getAssetFetchResult(fromAlbum album:String,
withLocalIdentifier localIdentifier:String) -> PHFetchResult<PHAsset>? {

let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [localIdentifier],
options: nil)
guard fetchResult.count > 0 else {
return nil
}
return fetchResult
}

fileprivate static func getAlbum(withName name:String) -> PHAssetCollection? {

let assetCollection = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: nil)
Expand Down

0 comments on commit baa7a16

Please sign in to comment.