2

Now that AssetsLibrary has been deprecated, we're supposed to use the photos framework, specifically PHPhotoLibrary to save images and videos to a users camera roll.

Using ReactiveCocoa, such a request would look like:

func saveImageAsAsset(url: NSURL) -> SignalProducer<String, NSError> {
    return SignalProducer { observer, disposable in
        var imageIdentifier: String?
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({
            let changeRequest = PHAssetChangeRequest.creationRequestForAssetFromImageAtFileURL(url)
            let placeholder = changeRequest?.placeholderForCreatedAsset
            imageIdentifier = placeholder?.localIdentifier
        }, completionHandler: { success, error in
            if let identifier = imageIdentifier where success {
                observer.sendNext(identifier)
            } else if let error = error {
                observer.sendFailed(error)
                return
            }
            observer.sendCompleted()
        })
    }
}

I created a gif from a video using Regift and I can verify that the gif exists inside my temporary directory. However when I go save that gif to the camera roll, I get a mysterious error: NSCocoaErrorDomain -1 (null), which is really super helpful.

Has anyone ever experienced this issue?

barndog
  • 6,314
  • 8
  • 45
  • 90

1 Answers1

0

You can try this.

let data = try? Data(contentsOf: /*Your-File-URL-Path*/)
PHPhotoLibrary.shared().performChanges({
    PHAssetCreationRequest.forAsset().addResource(with: .photo, data: data!, options: nil)
})
  • Welcome to SO! While this code may solve the problem in the question, we can improve this answer by adding a short explanation that describes why this code works. – Cy Rossignol Oct 27 '17 at 03:58