|
@@ -8,12 +8,12 @@
|
|
|
|
|
|
import AVFoundation
|
|
|
import CoreImage
|
|
|
-import CoreLocation
|
|
|
+//import CoreLocation
|
|
|
import CoreMotion
|
|
|
import ImageIO
|
|
|
import MobileCoreServices
|
|
|
-import Photos
|
|
|
-import PhotosUI
|
|
|
+//import Photos
|
|
|
+//import PhotosUI
|
|
|
import UIKit
|
|
|
|
|
|
public enum CameraState {
|
|
@@ -44,9 +44,9 @@ public enum CaptureResult {
|
|
|
self = .success(content: .imageData(data))
|
|
|
}
|
|
|
|
|
|
- init(_ asset: PHAsset) {
|
|
|
- self = .success(content: .asset(asset))
|
|
|
- }
|
|
|
+// init(_ asset: PHAsset) {
|
|
|
+// self = .success(content: .asset(asset))
|
|
|
+// }
|
|
|
|
|
|
var imageData: Data? {
|
|
|
if case let .success(content) = self {
|
|
@@ -60,7 +60,7 @@ public enum CaptureResult {
|
|
|
public enum CaptureContent {
|
|
|
case imageData(Data)
|
|
|
case image(UIImage)
|
|
|
- case asset(PHAsset)
|
|
|
+// case asset(PHAsset)
|
|
|
}
|
|
|
|
|
|
extension CaptureContent {
|
|
@@ -68,12 +68,12 @@ extension CaptureContent {
|
|
|
switch self {
|
|
|
case let .image(image): return image
|
|
|
case let .imageData(data): return UIImage(data: data)
|
|
|
- case let .asset(asset):
|
|
|
- if let data = getImageData(fromAsset: asset) {
|
|
|
- return UIImage(data: data)
|
|
|
- } else {
|
|
|
- return nil
|
|
|
- }
|
|
|
+// case let .asset(asset):
|
|
|
+// if let data = getImageData(fromAsset: asset) {
|
|
|
+// return UIImage(data: data)
|
|
|
+// } else {
|
|
|
+// return nil
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -81,22 +81,22 @@ extension CaptureContent {
|
|
|
switch self {
|
|
|
case let .image(image): return image.jpegData(compressionQuality: 1.0)
|
|
|
case let .imageData(data): return data
|
|
|
- case let .asset(asset): return getImageData(fromAsset: asset)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private func getImageData(fromAsset asset: PHAsset) -> Data? {
|
|
|
- var imageData: Data?
|
|
|
- let manager = PHImageManager.default()
|
|
|
- let options = PHImageRequestOptions()
|
|
|
- options.version = .original
|
|
|
- options.isSynchronous = true
|
|
|
- manager.requestImageData(for: asset, options: options) { data, _, _, _ in
|
|
|
-
|
|
|
- imageData = data
|
|
|
- }
|
|
|
- return imageData
|
|
|
- }
|
|
|
+// case let .asset(asset): return getImageData(fromAsset: asset)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// private func getImageData(fromAsset asset: PHAsset) -> Data? {
|
|
|
+// var imageData: Data?
|
|
|
+// let manager = PHImageManager.default()
|
|
|
+// let options = PHImageRequestOptions()
|
|
|
+// options.version = .original
|
|
|
+// options.isSynchronous = true
|
|
|
+// manager.requestImageData(for: asset, options: options) { data, _, _, _ in
|
|
|
+//
|
|
|
+// imageData = data
|
|
|
+// }
|
|
|
+// return imageData
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
public enum CaptureError: Error {
|
|
@@ -245,7 +245,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
open var shouldUseLocationServices: Bool = false {
|
|
|
didSet {
|
|
|
if shouldUseLocationServices {
|
|
|
- self.locationManager = CameraLocationManager()
|
|
|
+ //self.locationManager = CameraLocationManager()
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -336,7 +336,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
|
|
|
// MARK: - Private properties
|
|
|
|
|
|
- fileprivate var locationManager: CameraLocationManager?
|
|
|
+// fileprivate var locationManager: CameraLocationManager?
|
|
|
|
|
|
fileprivate weak var embeddingView: UIView?
|
|
|
fileprivate var videoCompletion: ((_ videoURL: URL?, _ error: NSError?) -> Void)?
|
|
@@ -358,7 +358,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
fileprivate var stillImageOutput: AVCaptureStillImageOutput?
|
|
|
fileprivate var movieOutput: AVCaptureMovieFileOutput?
|
|
|
fileprivate var previewLayer: AVCaptureVideoPreviewLayer?
|
|
|
- fileprivate var library: PHPhotoLibrary?
|
|
|
+// fileprivate var library: PHPhotoLibrary?
|
|
|
|
|
|
fileprivate var cameraIsSetup = false
|
|
|
fileprivate var cameraIsObservingDeviceOrientation = false
|
|
@@ -559,28 +559,28 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
let image = fixOrientation(withImage: img)
|
|
|
let newImageData = _imageDataWithEXIF(forImage: image, imageData)! as Data
|
|
|
|
|
|
- if writeFilesToPhoneLibrary {
|
|
|
- let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tempImg\(Int(Date().timeIntervalSince1970)).jpg")
|
|
|
-
|
|
|
- do {
|
|
|
- try newImageData.write(to: filePath)
|
|
|
-
|
|
|
- // make sure that doesn't fail the first time
|
|
|
- if PHPhotoLibrary.authorizationStatus() != .authorized {
|
|
|
- PHPhotoLibrary.requestAuthorization { status in
|
|
|
- if status == PHAuthorizationStatus.authorized {
|
|
|
- self._saveImageToLibrary(atFileURL: filePath, imageCompletion)
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- _saveImageToLibrary(atFileURL: filePath, imageCompletion)
|
|
|
- }
|
|
|
-
|
|
|
- } catch {
|
|
|
- imageCompletion(.failure(error))
|
|
|
- return
|
|
|
- }
|
|
|
- }
|
|
|
+// if writeFilesToPhoneLibrary {
|
|
|
+// let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tempImg\(Int(Date().timeIntervalSince1970)).jpg")
|
|
|
+//
|
|
|
+// do {
|
|
|
+// try newImageData.write(to: filePath)
|
|
|
+//
|
|
|
+// // make sure that doesn't fail the first time
|
|
|
+// if PHPhotoLibrary.authorizationStatus() != .authorized {
|
|
|
+// PHPhotoLibrary.requestAuthorization { status in
|
|
|
+// if status == PHAuthorizationStatus.authorized {
|
|
|
+// self._saveImageToLibrary(atFileURL: filePath, imageCompletion)
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// _saveImageToLibrary(atFileURL: filePath, imageCompletion)
|
|
|
+// }
|
|
|
+//
|
|
|
+// } catch {
|
|
|
+// imageCompletion(.failure(error))
|
|
|
+// return
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
imageCompletion(CaptureResult(newImageData))
|
|
|
}
|
|
@@ -606,9 +606,9 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
|
|
|
var mutableMetadata = CGImageMetadataCreateMutableCopy(imageProperties)!
|
|
|
|
|
|
- if let location = locationManager?.latestLocation {
|
|
|
- mutableMetadata = _gpsMetadata(mutableMetadata, withLocation: location)
|
|
|
- }
|
|
|
+// if let location = locationManager?.latestLocation {
|
|
|
+// mutableMetadata = _gpsMetadata(mutableMetadata, withLocation: location)
|
|
|
+// }
|
|
|
|
|
|
let finalMetadata: CGImageMetadata = mutableMetadata
|
|
|
CGImageDestinationAddImageAndMetadata(destination, UIImage(data: data)!.cgImage!, finalMetadata, nil)
|
|
@@ -643,15 +643,15 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
}
|
|
|
|
|
|
fileprivate func _saveImageToLibrary(atFileURL filePath: URL, _ imageCompletion: @escaping (CaptureResult) -> Void) {
|
|
|
- let location = locationManager?.latestLocation
|
|
|
- let date = Date()
|
|
|
-
|
|
|
- library?.save(imageAtURL: filePath, albumName: imageAlbumName, date: date, location: location) { asset in
|
|
|
-
|
|
|
- guard let _ = asset else {
|
|
|
+// let location = locationManager?.latestLocation
|
|
|
+// let date = Date()
|
|
|
+//
|
|
|
+// library?.save(imageAtURL: filePath, albumName: imageAlbumName, date: date, location: location) { asset in
|
|
|
+//
|
|
|
+// guard let _ = asset else {
|
|
|
return imageCompletion(.failure(CaptureError.assetNotSaved))
|
|
|
- }
|
|
|
- }
|
|
|
+// }
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -891,29 +891,29 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
if let error = error {
|
|
|
_show(NSLocalizedString("Unable to save video to the device", comment: ""), message: error.localizedDescription)
|
|
|
} else {
|
|
|
- if writeFilesToPhoneLibrary {
|
|
|
- if PHPhotoLibrary.authorizationStatus() == .authorized {
|
|
|
- _saveVideoToLibrary(outputFileURL)
|
|
|
- } else {
|
|
|
- PHPhotoLibrary.requestAuthorization { autorizationStatus in
|
|
|
- if autorizationStatus == .authorized {
|
|
|
- self._saveVideoToLibrary(outputFileURL)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
+// if writeFilesToPhoneLibrary {
|
|
|
+// if PHPhotoLibrary.authorizationStatus() == .authorized {
|
|
|
+// _saveVideoToLibrary(outputFileURL)
|
|
|
+// } else {
|
|
|
+// PHPhotoLibrary.requestAuthorization { autorizationStatus in
|
|
|
+// if autorizationStatus == .authorized {
|
|
|
+// self._saveVideoToLibrary(outputFileURL)
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
_executeVideoCompletionWithURL(outputFileURL, error: error as NSError?)
|
|
|
- }
|
|
|
+// }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fileprivate func _saveVideoToLibrary(_ fileURL: URL) {
|
|
|
- let location = locationManager?.latestLocation
|
|
|
- let date = Date()
|
|
|
-
|
|
|
- library?.save(videoAtURL: fileURL, albumName: videoAlbumName, date: date, location: location, completion: { _ in
|
|
|
- self._executeVideoCompletionWithURL(fileURL, error: nil)
|
|
|
- })
|
|
|
+// let location = locationManager?.latestLocation
|
|
|
+// let date = Date()
|
|
|
+//
|
|
|
+// library?.save(videoAtURL: fileURL, albumName: videoAlbumName, date: date, location: location, completion: { _ in
|
|
|
+// self._executeVideoCompletionWithURL(fileURL, error: nil)
|
|
|
+// })
|
|
|
}
|
|
|
|
|
|
// MARK: - UIGestureRecognizerDelegate
|
|
@@ -1307,9 +1307,9 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
currentConnection = stillImageOutput?.connection(with: AVMediaType.video)
|
|
|
case .videoOnly, .videoWithMic:
|
|
|
currentConnection = _getMovieOutput().connection(with: AVMediaType.video)
|
|
|
- if let location = locationManager?.latestLocation {
|
|
|
- _setVideoWithGPS(forLocation: location)
|
|
|
- }
|
|
|
+// if let location = locationManager?.latestLocation {
|
|
|
+// _setVideoWithGPS(forLocation: location)
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
if let validPreviewLayer = previewLayer {
|
|
@@ -1625,9 +1625,9 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
if movieOutput == nil {
|
|
|
movieOutput = _getMovieOutput()
|
|
|
}
|
|
|
- if library == nil {
|
|
|
- library = PHPhotoLibrary.shared()
|
|
|
- }
|
|
|
+// if library == nil {
|
|
|
+// library = PHPhotoLibrary.shared()
|
|
|
+// }
|
|
|
}
|
|
|
|
|
|
fileprivate func _setupPreviewLayer() {
|
|
@@ -1683,42 +1683,42 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
|
|
|
|
|
|
// MARK: - CameraLocationManager()
|
|
|
|
|
|
- public class CameraLocationManager: NSObject, CLLocationManagerDelegate {
|
|
|
- var locationManager = CLLocationManager()
|
|
|
- var latestLocation: CLLocation?
|
|
|
-
|
|
|
- override init() {
|
|
|
- super.init()
|
|
|
- locationManager.delegate = self
|
|
|
- locationManager.requestWhenInUseAuthorization()
|
|
|
- locationManager.distanceFilter = kCLDistanceFilterNone
|
|
|
- locationManager.headingFilter = 5.0
|
|
|
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
|
|
|
- }
|
|
|
-
|
|
|
- func startUpdatingLocation() {
|
|
|
- locationManager.startUpdatingLocation()
|
|
|
- }
|
|
|
-
|
|
|
- func stopUpdatingLocation() {
|
|
|
- locationManager.stopUpdatingLocation()
|
|
|
- }
|
|
|
-
|
|
|
- // MARK: - CLLocationManagerDelegate
|
|
|
-
|
|
|
- public func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
|
- // Pick the location with best (= smallest value) horizontal accuracy
|
|
|
- latestLocation = locations.sorted { $0.horizontalAccuracy < $1.horizontalAccuracy }.first
|
|
|
- }
|
|
|
-
|
|
|
- public func locationManager(_: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
|
|
|
- if status == .authorizedAlways || status == .authorizedWhenInUse {
|
|
|
- locationManager.startUpdatingLocation()
|
|
|
- } else {
|
|
|
- locationManager.stopUpdatingLocation()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+// public class CameraLocationManager: NSObject, CLLocationManagerDelegate {
|
|
|
+// var locationManager = CLLocationManager()
|
|
|
+// var latestLocation: CLLocation?
|
|
|
+//
|
|
|
+// override init() {
|
|
|
+// super.init()
|
|
|
+// locationManager.delegate = self
|
|
|
+// locationManager.requestWhenInUseAuthorization()
|
|
|
+// locationManager.distanceFilter = kCLDistanceFilterNone
|
|
|
+// locationManager.headingFilter = 5.0
|
|
|
+// locationManager.desiredAccuracy = kCLLocationAccuracyBest
|
|
|
+// }
|
|
|
+//
|
|
|
+// func startUpdatingLocation() {
|
|
|
+// locationManager.startUpdatingLocation()
|
|
|
+// }
|
|
|
+//
|
|
|
+// func stopUpdatingLocation() {
|
|
|
+// locationManager.stopUpdatingLocation()
|
|
|
+// }
|
|
|
+//
|
|
|
+// // MARK: - CLLocationManagerDelegate
|
|
|
+//
|
|
|
+// public func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
|
+// // Pick the location with best (= smallest value) horizontal accuracy
|
|
|
+// latestLocation = locations.sorted { $0.horizontalAccuracy < $1.horizontalAccuracy }.first
|
|
|
+// }
|
|
|
+//
|
|
|
+// public func locationManager(_: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
|
|
|
+// if status == .authorizedAlways || status == .authorizedWhenInUse {
|
|
|
+// locationManager.startUpdatingLocation()
|
|
|
+// } else {
|
|
|
+// locationManager.stopUpdatingLocation()
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
// Determining whether the current device actually supports blurring
|
|
|
// As seen on: http://stackoverflow.com/a/29997626/2269387
|
|
@@ -1955,167 +1955,167 @@ private extension AVCaptureDevice {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-extension PHPhotoLibrary {
|
|
|
- // MARK: - Public
|
|
|
-
|
|
|
- // finds or creates an album
|
|
|
-
|
|
|
- func getAlbum(name: String, completion: @escaping (PHAssetCollection) -> Void) {
|
|
|
- if let album = findAlbum(name: name) {
|
|
|
- completion(album)
|
|
|
- } else {
|
|
|
- createAlbum(name: name, completion: completion)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- func save(imageAtURL: URL, albumName: String?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
- func save() {
|
|
|
- if let albumName = albumName {
|
|
|
- getAlbum(name: albumName) { album in
|
|
|
- self.saveImage(imageAtURL: imageAtURL, album: album, date: date, location: location, completion: completion)
|
|
|
- }
|
|
|
- } else {
|
|
|
- saveImage(imageAtURL: imageAtURL, album: nil, date: date, location: location, completion: completion)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if PHPhotoLibrary.authorizationStatus() == .authorized {
|
|
|
- save()
|
|
|
- } else {
|
|
|
- PHPhotoLibrary.requestAuthorization { status in
|
|
|
- if status == .authorized {
|
|
|
- save()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- func save(videoAtURL: URL, albumName: String?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
- func save() {
|
|
|
- if let albumName = albumName {
|
|
|
- getAlbum(name: albumName) { album in
|
|
|
- self.saveVideo(videoAtURL: videoAtURL, album: album, date: date, location: location, completion: completion)
|
|
|
- }
|
|
|
- } else {
|
|
|
- saveVideo(videoAtURL: videoAtURL, album: nil, date: date, location: location, completion: completion)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if PHPhotoLibrary.authorizationStatus() == .authorized {
|
|
|
- save()
|
|
|
- } else {
|
|
|
- PHPhotoLibrary.requestAuthorization { status in
|
|
|
- if status == .authorized {
|
|
|
- save()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // MARK: - Private
|
|
|
-
|
|
|
- fileprivate func findAlbum(name: String) -> PHAssetCollection? {
|
|
|
- let fetchOptions = PHFetchOptions()
|
|
|
- fetchOptions.predicate = NSPredicate(format: "title = %@", name)
|
|
|
- let fetchResult: PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
|
|
|
- guard let photoAlbum = fetchResult.firstObject else {
|
|
|
- return nil
|
|
|
- }
|
|
|
- return photoAlbum
|
|
|
- }
|
|
|
-
|
|
|
- fileprivate func createAlbum(name: String, completion: @escaping (PHAssetCollection) -> Void) {
|
|
|
- var placeholder: PHObjectPlaceholder?
|
|
|
-
|
|
|
- performChanges({
|
|
|
- let createAlbumRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
|
|
|
- placeholder = createAlbumRequest.placeholderForCreatedAssetCollection
|
|
|
- }, completionHandler: { _, _ in
|
|
|
- let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeholder!.localIdentifier], options: nil)
|
|
|
- completion(fetchResult.firstObject!)
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- fileprivate func saveImage(imageAtURL: URL, album: PHAssetCollection?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
- var placeholder: PHObjectPlaceholder?
|
|
|
- performChanges({
|
|
|
- let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: imageAtURL)!
|
|
|
- createAssetRequest.creationDate = date
|
|
|
- createAssetRequest.location = location
|
|
|
- if let album = album {
|
|
|
- guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album),
|
|
|
- let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else { return }
|
|
|
- placeholder = photoPlaceholder
|
|
|
- let fastEnumeration = NSArray(array: [photoPlaceholder] as [PHObjectPlaceholder])
|
|
|
- albumChangeRequest.addAssets(fastEnumeration)
|
|
|
- }
|
|
|
-
|
|
|
- }, completionHandler: { success, _ in
|
|
|
- guard let placeholder = placeholder else {
|
|
|
- return
|
|
|
- }
|
|
|
- if success {
|
|
|
- let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
|
|
|
- let asset: PHAsset? = assets.firstObject
|
|
|
- completion?(asset)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- fileprivate func saveVideo(videoAtURL: URL, album: PHAssetCollection?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
- var placeholder: PHObjectPlaceholder?
|
|
|
- performChanges({
|
|
|
- let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoAtURL)!
|
|
|
- createAssetRequest.creationDate = date
|
|
|
- createAssetRequest.location = location
|
|
|
- if let album = album {
|
|
|
- guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album),
|
|
|
- let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else { return }
|
|
|
- placeholder = photoPlaceholder
|
|
|
- let fastEnumeration = NSArray(array: [photoPlaceholder] as [PHObjectPlaceholder])
|
|
|
- albumChangeRequest.addAssets(fastEnumeration)
|
|
|
- }
|
|
|
-
|
|
|
- }, completionHandler: { success, _ in
|
|
|
- guard let placeholder = placeholder else {
|
|
|
- completion?(nil)
|
|
|
- return
|
|
|
- }
|
|
|
- if success {
|
|
|
- let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
|
|
|
- let asset: PHAsset? = assets.firstObject
|
|
|
- completion?(asset)
|
|
|
- } else {
|
|
|
- completion?(nil)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- fileprivate func saveImage(image: UIImage, album: PHAssetCollection, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
- var placeholder: PHObjectPlaceholder?
|
|
|
- performChanges({
|
|
|
- let createAssetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
|
|
|
- createAssetRequest.creationDate = Date()
|
|
|
- guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album),
|
|
|
- let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else { return }
|
|
|
- placeholder = photoPlaceholder
|
|
|
- let fastEnumeration = NSArray(array: [photoPlaceholder] as [PHObjectPlaceholder])
|
|
|
- albumChangeRequest.addAssets(fastEnumeration)
|
|
|
- }, completionHandler: { success, _ in
|
|
|
- guard let placeholder = placeholder else {
|
|
|
- completion?(nil)
|
|
|
- return
|
|
|
- }
|
|
|
- if success {
|
|
|
- let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
|
|
|
- let asset: PHAsset? = assets.firstObject
|
|
|
- completion?(asset)
|
|
|
- } else {
|
|
|
- completion?(nil)
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-}
|
|
|
+//extension PHPhotoLibrary {
|
|
|
+// // MARK: - Public
|
|
|
+//
|
|
|
+// // finds or creates an album
|
|
|
+//
|
|
|
+// func getAlbum(name: String, completion: @escaping (PHAssetCollection) -> Void) {
|
|
|
+// if let album = findAlbum(name: name) {
|
|
|
+// completion(album)
|
|
|
+// } else {
|
|
|
+// createAlbum(name: name, completion: completion)
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// func save(imageAtURL: URL, albumName: String?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
+// func save() {
|
|
|
+// if let albumName = albumName {
|
|
|
+// getAlbum(name: albumName) { album in
|
|
|
+// self.saveImage(imageAtURL: imageAtURL, album: album, date: date, location: location, completion: completion)
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// saveImage(imageAtURL: imageAtURL, album: nil, date: date, location: location, completion: completion)
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// if PHPhotoLibrary.authorizationStatus() == .authorized {
|
|
|
+// save()
|
|
|
+// } else {
|
|
|
+// PHPhotoLibrary.requestAuthorization { status in
|
|
|
+// if status == .authorized {
|
|
|
+// save()
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// func save(videoAtURL: URL, albumName: String?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
+// func save() {
|
|
|
+// if let albumName = albumName {
|
|
|
+// getAlbum(name: albumName) { album in
|
|
|
+// self.saveVideo(videoAtURL: videoAtURL, album: album, date: date, location: location, completion: completion)
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// saveVideo(videoAtURL: videoAtURL, album: nil, date: date, location: location, completion: completion)
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// if PHPhotoLibrary.authorizationStatus() == .authorized {
|
|
|
+// save()
|
|
|
+// } else {
|
|
|
+// PHPhotoLibrary.requestAuthorization { status in
|
|
|
+// if status == .authorized {
|
|
|
+// save()
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // MARK: - Private
|
|
|
+//
|
|
|
+// fileprivate func findAlbum(name: String) -> PHAssetCollection? {
|
|
|
+// let fetchOptions = PHFetchOptions()
|
|
|
+// fetchOptions.predicate = NSPredicate(format: "title = %@", name)
|
|
|
+// let fetchResult: PHFetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: fetchOptions)
|
|
|
+// guard let photoAlbum = fetchResult.firstObject else {
|
|
|
+// return nil
|
|
|
+// }
|
|
|
+// return photoAlbum
|
|
|
+// }
|
|
|
+//
|
|
|
+// fileprivate func createAlbum(name: String, completion: @escaping (PHAssetCollection) -> Void) {
|
|
|
+// var placeholder: PHObjectPlaceholder?
|
|
|
+//
|
|
|
+// performChanges({
|
|
|
+// let createAlbumRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
|
|
|
+// placeholder = createAlbumRequest.placeholderForCreatedAssetCollection
|
|
|
+// }, completionHandler: { _, _ in
|
|
|
+// let fetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [placeholder!.localIdentifier], options: nil)
|
|
|
+// completion(fetchResult.firstObject!)
|
|
|
+// })
|
|
|
+// }
|
|
|
+//
|
|
|
+// fileprivate func saveImage(imageAtURL: URL, album: PHAssetCollection?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
+// var placeholder: PHObjectPlaceholder?
|
|
|
+// performChanges({
|
|
|
+// let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(atFileURL: imageAtURL)!
|
|
|
+// createAssetRequest.creationDate = date
|
|
|
+// createAssetRequest.location = location
|
|
|
+// if let album = album {
|
|
|
+// guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album),
|
|
|
+// let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else { return }
|
|
|
+// placeholder = photoPlaceholder
|
|
|
+// let fastEnumeration = NSArray(array: [photoPlaceholder] as [PHObjectPlaceholder])
|
|
|
+// albumChangeRequest.addAssets(fastEnumeration)
|
|
|
+// }
|
|
|
+//
|
|
|
+// }, completionHandler: { success, _ in
|
|
|
+// guard let placeholder = placeholder else {
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if success {
|
|
|
+// let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
|
|
|
+// let asset: PHAsset? = assets.firstObject
|
|
|
+// completion?(asset)
|
|
|
+// }
|
|
|
+// })
|
|
|
+// }
|
|
|
+//
|
|
|
+// fileprivate func saveVideo(videoAtURL: URL, album: PHAssetCollection?, date: Date = Date(), location: CLLocation? = nil, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
+// var placeholder: PHObjectPlaceholder?
|
|
|
+// performChanges({
|
|
|
+// let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoAtURL)!
|
|
|
+// createAssetRequest.creationDate = date
|
|
|
+// createAssetRequest.location = location
|
|
|
+// if let album = album {
|
|
|
+// guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album),
|
|
|
+// let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else { return }
|
|
|
+// placeholder = photoPlaceholder
|
|
|
+// let fastEnumeration = NSArray(array: [photoPlaceholder] as [PHObjectPlaceholder])
|
|
|
+// albumChangeRequest.addAssets(fastEnumeration)
|
|
|
+// }
|
|
|
+//
|
|
|
+// }, completionHandler: { success, _ in
|
|
|
+// guard let placeholder = placeholder else {
|
|
|
+// completion?(nil)
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if success {
|
|
|
+// let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
|
|
|
+// let asset: PHAsset? = assets.firstObject
|
|
|
+// completion?(asset)
|
|
|
+// } else {
|
|
|
+// completion?(nil)
|
|
|
+// }
|
|
|
+// })
|
|
|
+// }
|
|
|
+//
|
|
|
+// fileprivate func saveImage(image: UIImage, album: PHAssetCollection, completion: ((PHAsset?) -> Void)? = nil) {
|
|
|
+// var placeholder: PHObjectPlaceholder?
|
|
|
+// performChanges({
|
|
|
+// let createAssetRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
|
|
|
+// createAssetRequest.creationDate = Date()
|
|
|
+// guard let albumChangeRequest = PHAssetCollectionChangeRequest(for: album),
|
|
|
+// let photoPlaceholder = createAssetRequest.placeholderForCreatedAsset else { return }
|
|
|
+// placeholder = photoPlaceholder
|
|
|
+// let fastEnumeration = NSArray(array: [photoPlaceholder] as [PHObjectPlaceholder])
|
|
|
+// albumChangeRequest.addAssets(fastEnumeration)
|
|
|
+// }, completionHandler: { success, _ in
|
|
|
+// guard let placeholder = placeholder else {
|
|
|
+// completion?(nil)
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if success {
|
|
|
+// let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: nil)
|
|
|
+// let asset: PHAsset? = assets.firstObject
|
|
|
+// completion?(asset)
|
|
|
+// } else {
|
|
|
+// completion?(nil)
|
|
|
+// }
|
|
|
+// })
|
|
|
+// }
|
|
|
+//}
|
|
|
|
|
|
extension CameraManager: AVCaptureMetadataOutputObjectsDelegate {
|
|
|
/**
|