简体   繁体   中英

iOS 9 save gif to photo library

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?

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)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM