Skip to content

Commit 61812a4

Browse files
committed
Fixed issues remirobert#76(crashing due to nil preview layer) and remirobert#67(crashing on second photo capture due to old settings object).
1 parent eeb8a73 commit 61812a4

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

CameraEngine/CameraEngine.swift

+28-26
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class CameraEngine: NSObject {
101101
}
102102
}
103103

104-
public lazy var previewLayer: AVCaptureVideoPreviewLayer! = {
104+
public lazy var previewLayer: AVCaptureVideoPreviewLayer? = {
105105
let layer = AVCaptureVideoPreviewLayer(session: self.session)
106106
layer?.videoGravity = AVLayerVideoGravityResizeAspectFill
107107
return layer
@@ -300,7 +300,7 @@ public class CameraEngine: NSObject {
300300
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
301301
}
302302
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIDeviceOrientationDidChange, object: nil, queue: OperationQueue.main) { (_) -> Void in
303-
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.orientationFromUIDeviceOrientation(UIDevice.current.orientation)
303+
self.previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.orientationFromUIDeviceOrientation(UIDevice.current.orientation)
304304
}
305305
}
306306
else {
@@ -423,31 +423,32 @@ public extension CameraEngine {
423423
let performFocus = currentDevice.isFocusModeSupported(.autoFocus) && currentDevice.isFocusPointOfInterestSupported
424424
let performExposure = currentDevice.isExposureModeSupported(.autoExpose) && currentDevice.isExposurePointOfInterestSupported
425425
if performFocus || performExposure {
426-
let focusPoint = self.previewLayer.captureDevicePointOfInterest(for: atPoint)
427-
do {
428-
try currentDevice.lockForConfiguration()
429-
430-
if performFocus {
431-
currentDevice.focusPointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
432-
if currentDevice.focusMode == AVCaptureFocusMode.locked {
433-
currentDevice.focusMode = AVCaptureFocusMode.autoFocus
434-
} else {
435-
currentDevice.focusMode = AVCaptureFocusMode.continuousAutoFocus
436-
}
437-
}
438-
439-
if performExposure {
440-
currentDevice.exposurePointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
441-
if currentDevice.exposureMode == AVCaptureExposureMode.locked {
442-
currentDevice.exposureMode = AVCaptureExposureMode.autoExpose
443-
} else {
444-
currentDevice.exposureMode = AVCaptureExposureMode.continuousAutoExposure;
426+
if let focusPoint = self.previewLayer?.captureDevicePointOfInterest(for: atPoint) {
427+
do {
428+
try currentDevice.lockForConfiguration()
429+
430+
if performFocus {
431+
currentDevice.focusPointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
432+
if currentDevice.focusMode == AVCaptureFocusMode.locked {
433+
currentDevice.focusMode = AVCaptureFocusMode.autoFocus
434+
} else {
435+
currentDevice.focusMode = AVCaptureFocusMode.continuousAutoFocus
436+
}
437+
}
438+
439+
if performExposure {
440+
currentDevice.exposurePointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
441+
if currentDevice.exposureMode == AVCaptureExposureMode.locked {
442+
currentDevice.exposureMode = AVCaptureExposureMode.autoExpose
443+
} else {
444+
currentDevice.exposureMode = AVCaptureExposureMode.continuousAutoExposure;
445+
}
445446
}
447+
currentDevice.unlockForConfiguration()
448+
}
449+
catch {
450+
fatalError("[CameraEngine] error lock configuration device")
446451
}
447-
currentDevice.unlockForConfiguration()
448-
}
449-
catch {
450-
fatalError("[CameraEngine] error lock configuration device")
451452
}
452453
}
453454
}
@@ -459,7 +460,8 @@ public extension CameraEngine {
459460
public extension CameraEngine {
460461

461462
public func capturePhoto(_ blockCompletion: @escaping blockCompletionCapturePhoto) {
462-
self.cameraOutput.capturePhoto(settings: self.capturePhotoSettings, blockCompletion)
463+
let uniqueSettings = AVCapturePhotoSettings.init(from: self.capturePhotoSettings)
464+
self.cameraOutput.capturePhoto(settings: uniqueSettings, blockCompletion)
463465
}
464466

465467
public func capturePhotoBuffer(_ blockCompletion: @escaping blockCompletionCapturePhotoBuffer) {

CameraEngineExample/CameraEngineExample.xcodeproj/project.pbxproj

+25-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
2FE9684B1E92634900437A67 /* CameraEngine.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2FE9684A1E92634900437A67 /* CameraEngine.framework */; };
11+
2FE9684C1E92634900437A67 /* CameraEngine.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 2FE9684A1E92634900437A67 /* CameraEngine.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
1012
BB3B05D31D8C242A00165E2C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3B05D21D8C242A00165E2C /* AppDelegate.swift */; };
1113
BB3B05D51D8C242A00165E2C /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3B05D41D8C242A00165E2C /* ViewController.swift */; };
1214
BB3B05D81D8C242A00165E2C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BB3B05D61D8C242A00165E2C /* Main.storyboard */; };
@@ -15,7 +17,22 @@
1517
BB9DFAD61D8C4D95001E7B35 /* CameraEngine.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BB9DFAD51D8C4D95001E7B35 /* CameraEngine.framework */; };
1618
/* End PBXBuildFile section */
1719

20+
/* Begin PBXCopyFilesBuildPhase section */
21+
2FE9684D1E92634900437A67 /* Embed Frameworks */ = {
22+
isa = PBXCopyFilesBuildPhase;
23+
buildActionMask = 2147483647;
24+
dstPath = "";
25+
dstSubfolderSpec = 10;
26+
files = (
27+
2FE9684C1E92634900437A67 /* CameraEngine.framework in Embed Frameworks */,
28+
);
29+
name = "Embed Frameworks";
30+
runOnlyForDeploymentPostprocessing = 0;
31+
};
32+
/* End PBXCopyFilesBuildPhase section */
33+
1834
/* Begin PBXFileReference section */
35+
2FE9684A1E92634900437A67 /* CameraEngine.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = CameraEngine.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1936
BB3B05CF1D8C242A00165E2C /* CameraEngineExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CameraEngineExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
2037
BB3B05D21D8C242A00165E2C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
2138
BB3B05D41D8C242A00165E2C /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
@@ -32,6 +49,7 @@
3249
buildActionMask = 2147483647;
3350
files = (
3451
BB9DFAD61D8C4D95001E7B35 /* CameraEngine.framework in Frameworks */,
52+
2FE9684B1E92634900437A67 /* CameraEngine.framework in Frameworks */,
3553
);
3654
runOnlyForDeploymentPostprocessing = 0;
3755
};
@@ -41,6 +59,7 @@
4159
BB3B05C61D8C242900165E2C = {
4260
isa = PBXGroup;
4361
children = (
62+
2FE9684A1E92634900437A67 /* CameraEngine.framework */,
4463
BB3B05D11D8C242A00165E2C /* CameraEngineExample */,
4564
BB3B05D01D8C242A00165E2C /* Products */,
4665
BB9DFAD41D8C4D95001E7B35 /* Frameworks */,
@@ -86,6 +105,7 @@
86105
BB3B05CB1D8C242A00165E2C /* Sources */,
87106
BB3B05CC1D8C242A00165E2C /* Frameworks */,
88107
BB3B05CD1D8C242A00165E2C /* Resources */,
108+
2FE9684D1E92634900437A67 /* Embed Frameworks */,
89109
);
90110
buildRules = (
91111
);
@@ -108,7 +128,7 @@
108128
TargetAttributes = {
109129
BB3B05CE1D8C242A00165E2C = {
110130
CreatedOnToolsVersion = 8.0;
111-
DevelopmentTeam = 44Q2J6374R;
131+
DevelopmentTeam = 2R66XDN32F;
112132
ProvisioningStyle = Automatic;
113133
};
114134
};
@@ -273,7 +293,8 @@
273293
isa = XCBuildConfiguration;
274294
buildSettings = {
275295
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
276-
DEVELOPMENT_TEAM = 44Q2J6374R;
296+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
297+
DEVELOPMENT_TEAM = 2R66XDN32F;
277298
INFOPLIST_FILE = CameraEngineExample/Info.plist;
278299
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
279300
PRODUCT_BUNDLE_IDENTIFIER = com.remirobert.CameraEngineExample;
@@ -286,7 +307,8 @@
286307
isa = XCBuildConfiguration;
287308
buildSettings = {
288309
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
289-
DEVELOPMENT_TEAM = 44Q2J6374R;
310+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
311+
DEVELOPMENT_TEAM = 2R66XDN32F;
290312
INFOPLIST_FILE = CameraEngineExample/Info.plist;
291313
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
292314
PRODUCT_BUNDLE_IDENTIFIER = com.remirobert.CameraEngineExample;

0 commit comments

Comments
 (0)