Skip to content

Instantly share code, notes, and snippets.

@gunantosteven
Last active April 14, 2020 13:31
Show Gist options
  • Save gunantosteven/fd583754ee81eb482f82899fa0004f5a to your computer and use it in GitHub Desktop.
Save gunantosteven/fd583754ee81eb482f82899fa0004f5a to your computer and use it in GitHub Desktop.
Be Careful with "UI API called on a background thread"
self.view.makeToastActivity(.center)
DispatchQueue.global(qos: .background).async {
if let url = URL(string: self.photo.url),
let urlData = NSData(contentsOf: url)
{
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let filePath="\(documentsPath)/tempFile.mp4"
urlData.write(toFile: filePath, atomically: true)
PHPhotoLibrary.shared().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: filePath))
let albumChangeRequest = PHAssetCollectionChangeRequest(for: CustomPhotoAlbum.sharedInstance.assetCollection)
albumChangeRequest!.addAssets([assetChangeRequest!.placeholderForCreatedAsset!] as NSArray)
}) { completed, error in
if completed {
print("Video is saved!")
}
DispatchQueue.main.async {
self.view.hideToastActivity()
self.view.makeToast(self.messages.social_downloaded, duration: 2.0, position: .center)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment