From 39bd8589fe507aaa2cc1ced80dc847f1f19148f6 Mon Sep 17 00:00:00 2001 From: geegaset Date: Mon, 10 Oct 2016 12:05:20 +0100 Subject: [PATCH] Avoids using CaptureSession in simulator (#234) App crashes on iOS 10 simulator if so --- .../Photos/LiveCameraCaptureSession.swift | 28 +++++++++++-------- .../Photos/PhotosInputDataProvider.swift | 3 +- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/ChattoAdditions/Source/Input/Photos/LiveCameraCaptureSession.swift b/ChattoAdditions/Source/Input/Photos/LiveCameraCaptureSession.swift index 35c75fa..67214ae 100644 --- a/ChattoAdditions/Source/Input/Photos/LiveCameraCaptureSession.swift +++ b/ChattoAdditions/Source/Input/Photos/LiveCameraCaptureSession.swift @@ -38,7 +38,7 @@ class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol { var isInitialized: Bool = false var isCapturing: Bool { - return self.isInitialized && self.captureSession.running + return self.isInitialized && self.captureSession?.running ?? false } deinit { @@ -57,7 +57,7 @@ class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol { operation.addExecutionBlock { [weak operation, weak self] in guard let sSelf = self, strongOperation = operation where !strongOperation.cancelled else { return } sSelf.addInputDevicesIfNeeded() - sSelf.captureSession.startRunning() + sSelf.captureSession?.startRunning() dispatch_async(dispatch_get_main_queue(), completion) } self.queue.cancelAllOperations() @@ -68,7 +68,7 @@ class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol { let operation = NSBlockOperation() operation.addExecutionBlock { [weak operation, weak self] in guard let sSelf = self, strongOperation = operation where !strongOperation.cancelled else { return } - sSelf.captureSession.stopRunning() + sSelf.captureSession?.stopRunning() sSelf.removeInputDevices() dispatch_async(dispatch_get_main_queue(), completion) } @@ -85,23 +85,27 @@ class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol { return queue }() - private lazy var captureSession: AVCaptureSession = { + private lazy var captureSession: AVCaptureSession? = { assert(!NSThread.isMainThread(), "This can be very slow, make sure it happens in a background thread") - let session = AVCaptureSession() - self.captureLayer = AVCaptureVideoPreviewLayer(session: session) - self.captureLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill self.isInitialized = true - return session + #if !(arch(i386) || arch(x86_64)) + let session = AVCaptureSession() + self.captureLayer = AVCaptureVideoPreviewLayer(session: session) + self.captureLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill + return session + #else + return nil + #endif }() private func addInputDevicesIfNeeded() { assert(!NSThread.isMainThread(), "This can be very slow, make sure it happens in a background thread") - if self.captureSession.inputs?.count == 0 { + if self.captureSession?.inputs?.count == 0 { let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) do { let input = try AVCaptureDeviceInput(device: device) - self.captureSession.addInput(input) + self.captureSession?.addInput(input) } catch { } @@ -110,8 +114,8 @@ class LiveCameraCaptureSession: LiveCameraCaptureSessionProtocol { private func removeInputDevices() { assert(!NSThread.isMainThread(), "This can be very slow, make sure it happens in a background thread") - self.captureSession.inputs?.forEach { (input) in - self.captureSession.removeInput(input as! AVCaptureInput) + self.captureSession?.inputs?.forEach { (input) in + self.captureSession?.removeInput(input as! AVCaptureInput) } } } diff --git a/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift b/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift index 6e99eb3..63bf7c3 100644 --- a/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift +++ b/ChattoAdditions/Source/Input/Photos/PhotosInputDataProvider.swift @@ -75,8 +75,7 @@ class PhotosInputDataProvider: NSObject, PhotosInputDataProviderProtocol, PHPhot if let userLibraryCollection = PHAssetCollection.fetchAssetCollectionsWithType(.SmartAlbum, subtype: .SmartAlbumUserLibrary, options: nil).firstObject as? PHAssetCollection { self.fetchResult = PHAsset.fetchAssetsInAssetCollection(userLibraryCollection, options: fetchOptions(NSPredicate(format: "mediaType = \(PHAssetMediaType.Image.rawValue)"))) - } - else { + } else { self.fetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions(nil)) } super.init()