How to get the URL of an image just added in PHPhotoLibrary

后端 未结 6 887
臣服心动
臣服心动 2020-12-30 03:11

I am using the UIImagePickerController in two cases

  • to select an existing image in the Photo Library
  • to take a new picture

In the first

6条回答
  •  别那么骄傲
    2020-12-30 04:01

    Swift 4 solution that works for me to get the url of last image in Photo Library:

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
    let fetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions).lastObject
    PHImageManager().requestAVAsset(forVideo: fetchResult!, options: nil, resultHandler { (avurlAsset, _, _) in
        if let newObj = avurlAsset as? AVURLAsset {
            print(newObj.url)
        }
    })
    

    Hope it helps!

提交回复
热议问题