mirror of
https://github.com/jphong1111/awesome-ios-developer.git
synced 2025-01-31 01:44:55 +07:00
Add files via upload
This commit is contained in:
parent
0355170854
commit
6e9ccfa47b
41
Helper/VideoManager.swift
Normal file
41
Helper/VideoManager.swift
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
//
|
||||||
|
// VideoManager.swift
|
||||||
|
// DispatchDemoApp
|
||||||
|
//
|
||||||
|
// Created by JungpyoHong on 4/29/21.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import PhotosUI
|
||||||
|
|
||||||
|
struct VideoManager {
|
||||||
|
|
||||||
|
func downloadVideoLinkAndCreateAsset(_ videoLink: String) {
|
||||||
|
|
||||||
|
guard let videoURL = URL(string: videoLink) else { return }
|
||||||
|
guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
|
||||||
|
if !FileManager.default.fileExists(atPath: documentsDirectoryURL.appendingPathComponent(videoURL.lastPathComponent).path) {
|
||||||
|
URLSession.shared.downloadTask(with: videoURL) { (location, response, error) -> Void in
|
||||||
|
guard let location = location else { return }
|
||||||
|
let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggestedFilename ?? videoURL.lastPathComponent)
|
||||||
|
do {
|
||||||
|
try FileManager.default.moveItem(at: location, to: destinationURL)
|
||||||
|
PHPhotoLibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> Void in
|
||||||
|
if authorizationStatus == .authorized {
|
||||||
|
PHPhotoLibrary.shared().performChanges({
|
||||||
|
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed, error in
|
||||||
|
if completed {
|
||||||
|
print("Video asset created")
|
||||||
|
} else {
|
||||||
|
print("error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch { print(error) }
|
||||||
|
}.resume()
|
||||||
|
} else {
|
||||||
|
print("File already exists at destination url")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user