private var fetchResult: PHFetchResult<PHAsset>!
func picker(_ picker: PHPickerViewController, didFinishPicking
results: [PHPickerResult]) {
self.dismiss(animated: true, completion: nil)
if let userLibraryCollection =
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype:
.smartAlbumUserLibrary, options: nil).firstObject {
self.fetchResult = PHAsset.fetchAssets(in:
userLibraryCollection, options: fetchOptions(NSPredicate(format:
"mediaType = \(PHAssetMediaType.image.rawValue)")))
fetchResult.enumerateObjects { asset, index, stop in
PHAsset.getURL(ofPhotoWith: asset) { (url) in
if let imgurl = url{
print(imgurl)
}else {
print("error")
}
}
}
} else {
self.fetchResult = PHAsset.fetchAssets(with: .image, options:
fetchOptions(nil))
print(fetchResult.firstObject)
}
}
extension PHAsset {
static func getURL(ofPhotoWith mPhasset: PHAsset, completionHandler : @escaping ((_ responseURL : URL?) -> Void)) {
if mPhasset.mediaType == .image {
let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -> Bool in
return true
}
mPhasset.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput, info) in
if let fullSizeImageUrl = contentEditingInput?.fullSizeImageURL {
completionHandler(fullSizeImageUrl)
} else {
completionHandler(nil)
}
})
} else if mPhasset.mediaType == .video {
let options: PHVideoRequestOptions = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: mPhasset, options: options, resultHandler: { (asset, audioMix, info) in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl = urlAsset.url
completionHandler(localVideoUrl)
} else {
completionHandler(nil)
}
})
}
}
}