-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLivePhotoService.swift
More file actions
50 lines (46 loc) · 2.07 KB
/
Copy pathLivePhotoService.swift
File metadata and controls
50 lines (46 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Photos
enum LivePhotoService {
static func requestLivePhoto(photoURL: URL, videoURL: URL) async throws -> PHLivePhoto {
try await withCheckedThrowingContinuation { continuation in
PHLivePhoto.request(
withResourceFileURLs: [photoURL, videoURL],
placeholderImage: nil,
targetSize: .zero,
contentMode: .aspectFit
) { livePhoto, info in
if let error = info[PHLivePhotoInfoErrorKey] as? Error {
continuation.resume(throwing: error)
return
}
guard let livePhoto else {
continuation.resume(throwing: SpikeError.livePhotoRequestFailed)
return
}
continuation.resume(returning: livePhoto)
}
}
}
static func saveToPhotos(photoURL: URL, videoURL: URL) async throws {
let status = await PHPhotoLibrary.requestAuthorization(for: .readWrite)
guard status == .authorized || status == .limited else {
throw SpikeError.photosDenied
}
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
PHPhotoLibrary.shared().performChanges {
let request = PHAssetCreationRequest.forAsset()
let photoOptions = PHAssetResourceCreationOptions()
photoOptions.shouldMoveFile = false
request.addResource(with: .photo, fileURL: photoURL, options: photoOptions)
let videoOptions = PHAssetResourceCreationOptions()
videoOptions.shouldMoveFile = false
request.addResource(with: .pairedVideo, fileURL: videoURL, options: videoOptions)
} completionHandler: { success, error in
if success {
continuation.resume()
} else {
continuation.resume(throwing: SpikeError.saveFailed(error?.localizedDescription ?? "unknown"))
}
}
}
}
}