1

I am trying to save a video to Gallery and after that retrieving the asset, in ios 12 it worked fine, now testing it in ios 13 is not working anymore. The video is correctly saved, but the asset retrieved is not the correct one, instead, it is another from the library.

PHPhotoLibrary.shared().performChanges({
    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: filePath)
}) { completed, error in
    if completed {
        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

        let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).lastObject
        GeneralUtils().shareVideoFacebook(fetchResult!, self)
    }

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
David Vittori
  • 1,146
  • 1
  • 13
  • 30
  • Creation date is not the way to do this. If you wanted to access the asset after creation, you should not have thrown away the result of the creation request. – matt Nov 08 '19 at 16:12
  • Also the video is presumably still at the file url so why bother fetching it from the library when you still have it in your hands? – matt Nov 08 '19 at 16:14
  • The problem is that FBSDK wants an Asset URL to share the video, I have the video at an internal directory – David Vittori Nov 08 '19 at 16:17

2 Answers2

0

I just found the solution to this problem. You can ask for a local identifier of the saved image.

var id: String?
PHPhotoLibrary.shared().performChanges({
    let collection = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: filePath)
    id = collection?.placeholderForCreatedAsset?.localIdentifier
}) { completed, error in
    if completed {
        let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [id!], options: .none).firstObject
        GeneralUtils().shareVideoFacebook(fetchResult!, self)
    }                        
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
David Vittori
  • 1,146
  • 1
  • 13
  • 30
  • Yes that’s what I said. The create request returns a value but you were throwing it away. – matt Nov 08 '19 at 18:16
0
   DispatchQueue.global(qos: .background).async {
        if let url = URL(string: url),
           let urlData = NSData(contentsOf: url)
        { let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let filePath="\(documentsPath)/file.mp4"
            DispatchQueue.main.async {
                urlData.write(toFile: filePath, atomically: true)
                PHPhotoLibrary.shared().performChanges({
            self.instagramVideoId =  PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: filePath))?.placeholderForCreatedAsset?.localIdentifier
                }) { completed, _ in
                    if completed {
                        let url = URL(string: "instagram://library?LocalIdentifier=\(self.instagramVideoId!)")!
                        DispatchQueue.main.async {
                            if UIApplication.shared.canOpenURL(url) {
                                UIApplication.shared.open(url)
                            } else {
                            }
                        }
                    }
                }
            }
        }
        self.hideLoader()
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135