1

I need to export a video from my iOS app and get the asset id. In the past, you can use ALAssetsLibrary:

save image in camera roll and get asset url

But it's deprecated now. I wonder what would be the new way of achieving this now? Thanks!

Thinium
  • 171
  • 1
  • 14

1 Answers1

0

ALAssetsLibrary is deprecated. you should use PhotosLibrary.

The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library. For more information, see Photos. In the Photos framework, the PHPhotoLibrary class manages access to and changes in the photo library, and class methods on the PHAsset and PHCollection classes and related classes provide functionality for finding photo and video assets.

See this https://developer.apple.com/documentation/photos/phphotolibrary

here is a sample code that saves the video to camera roll and gives you the url to the save video.

var placeHolder : PHObjectPlaceholder?
PHPhotoLibrary.shared().performChanges({

    let creationRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(string: videoPath)! )
    placeHolder = creationRequest?.placeholderForCreatedAsset

    }, completionHandler: { (success, error) in
        if success {

            let result = PHAsset.fetchAssets(withLocalIdentifiers: [placeHolder!.localIdentifier], options: nil)
            result.firstObject?.getURL(completionHandler: { url in

                // this is the url to the saved asset 

            })
        }
})

don't forget to

import Photos

and add this code as an extension to PHAsset :

https://gist.github.com/jVirus/2f041bb8b1936093773f0bde42af3a49

SRTWarrior
  • 43
  • 9
  • check this out, as ios 11 you can use assetResources instead of fetch assets. https://stackoverflow.com/a/51740329/4264079 – SRTWarrior Jul 30 '19 at 06:44