Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Last active July 21, 2016 09:52
Show Gist options
  • Save quangtqag/adf15003e215f00ff8e5dfeebced0cf4 to your computer and use it in GitHub Desktop.
Save quangtqag/adf15003e215f00ff8e5dfeebced0cf4 to your computer and use it in GitHub Desktop.
import Photos
func saveVideoIntoPhotosAppWithURL(videoURL: NSURL, completionHandler: ()->(), errorHandler: (error: NSError)->()) {
switch PHPhotoLibrary.authorizationStatus() {
case PHAuthorizationStatus.NotDetermined:
PHPhotoLibrary.requestAuthorization({ (authorizationStatus) in
dispatch_async(dispatch_get_main_queue(), {
self.saveVideoIntoPhotosAppWithURL(videoURL, completionHandler: completionHandler, errorHandler: errorHandler)
})
})
break
case PHAuthorizationStatus.Authorized:
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(videoURL)
self.view.userInteractionEnabled = false
}) { saved, error in
dispatch_async(dispatch_get_main_queue(), {
self.view.userInteractionEnabled = true
if saved {
let hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
hud.mode = MBProgressHUDMode.Text
hud.labelText = RS_SAVED_TO_GALLERY
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue(), {
hud.hide(true)
})
completionHandler()
}
else if error != nil {
errorHandler(error: error!)
}
})
}
break
case PHAuthorizationStatus.Denied:
let alertController = UIAlertController(title: RS_SAVE_VIDEO_TO_YOUR_PHOTOS, message: RS_NEED_RIGHT_ACCESS_PHOTOS, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: RS_NOT_NOW, style: .Cancel, handler: nil))
alertController.addAction(UIAlertAction(title: RS_YES, style: UIAlertActionStyle.Default, handler: { (alertAction) in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}))
self.presentViewController(alertController, animated: true, completion: nil)
break
default:
break
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment