Skip to content

Commit

Permalink
Fixed issues remirobert#76(crashing due to nil preview layer) and rem…
Browse files Browse the repository at this point in the history
…irobert#67(crashing on second photo capture due to old settings object).
  • Loading branch information
ihak committed Apr 3, 2017
1 parent eeb8a73 commit 61812a4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
54 changes: 28 additions & 26 deletions CameraEngine/CameraEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class CameraEngine: NSObject {
}
}

public lazy var previewLayer: AVCaptureVideoPreviewLayer! = {
public lazy var previewLayer: AVCaptureVideoPreviewLayer? = {
let layer = AVCaptureVideoPreviewLayer(session: self.session)
layer?.videoGravity = AVLayerVideoGravityResizeAspectFill
return layer
Expand Down Expand Up @@ -300,7 +300,7 @@ public class CameraEngine: NSObject {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIDeviceOrientationDidChange, object: nil, queue: OperationQueue.main) { (_) -> Void in
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.orientationFromUIDeviceOrientation(UIDevice.current.orientation)
self.previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.orientationFromUIDeviceOrientation(UIDevice.current.orientation)
}
}
else {
Expand Down Expand Up @@ -423,31 +423,32 @@ public extension CameraEngine {
let performFocus = currentDevice.isFocusModeSupported(.autoFocus) && currentDevice.isFocusPointOfInterestSupported
let performExposure = currentDevice.isExposureModeSupported(.autoExpose) && currentDevice.isExposurePointOfInterestSupported
if performFocus || performExposure {
let focusPoint = self.previewLayer.captureDevicePointOfInterest(for: atPoint)
do {
try currentDevice.lockForConfiguration()

if performFocus {
currentDevice.focusPointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
if currentDevice.focusMode == AVCaptureFocusMode.locked {
currentDevice.focusMode = AVCaptureFocusMode.autoFocus
} else {
currentDevice.focusMode = AVCaptureFocusMode.continuousAutoFocus
}
}

if performExposure {
currentDevice.exposurePointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
if currentDevice.exposureMode == AVCaptureExposureMode.locked {
currentDevice.exposureMode = AVCaptureExposureMode.autoExpose
} else {
currentDevice.exposureMode = AVCaptureExposureMode.continuousAutoExposure;
if let focusPoint = self.previewLayer?.captureDevicePointOfInterest(for: atPoint) {
do {
try currentDevice.lockForConfiguration()

if performFocus {
currentDevice.focusPointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
if currentDevice.focusMode == AVCaptureFocusMode.locked {
currentDevice.focusMode = AVCaptureFocusMode.autoFocus
} else {
currentDevice.focusMode = AVCaptureFocusMode.continuousAutoFocus
}
}

if performExposure {
currentDevice.exposurePointOfInterest = CGPoint(x: focusPoint.x, y: focusPoint.y)
if currentDevice.exposureMode == AVCaptureExposureMode.locked {
currentDevice.exposureMode = AVCaptureExposureMode.autoExpose
} else {
currentDevice.exposureMode = AVCaptureExposureMode.continuousAutoExposure;
}
}
currentDevice.unlockForConfiguration()
}
catch {
fatalError("[CameraEngine] error lock configuration device")
}
currentDevice.unlockForConfiguration()
}
catch {
fatalError("[CameraEngine] error lock configuration device")
}
}
}
Expand All @@ -459,7 +460,8 @@ public extension CameraEngine {
public extension CameraEngine {

public func capturePhoto(_ blockCompletion: @escaping blockCompletionCapturePhoto) {
self.cameraOutput.capturePhoto(settings: self.capturePhotoSettings, blockCompletion)
let uniqueSettings = AVCapturePhotoSettings.init(from: self.capturePhotoSettings)
self.cameraOutput.capturePhoto(settings: uniqueSettings, blockCompletion)
}

public func capturePhotoBuffer(_ blockCompletion: @escaping blockCompletionCapturePhotoBuffer) {
Expand Down
28 changes: 25 additions & 3 deletions CameraEngineExample/CameraEngineExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

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

/* Begin PBXCopyFilesBuildPhase section */
2FE9684D1E92634900437A67 /* Embed Frameworks */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 10;
files = (
2FE9684C1E92634900437A67 /* CameraEngine.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2FE9684A1E92634900437A67 /* CameraEngine.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = CameraEngine.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BB3B05CF1D8C242A00165E2C /* CameraEngineExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CameraEngineExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
BB3B05D21D8C242A00165E2C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
BB3B05D41D8C242A00165E2C /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand All @@ -32,6 +49,7 @@
buildActionMask = 2147483647;
files = (
BB9DFAD61D8C4D95001E7B35 /* CameraEngine.framework in Frameworks */,
2FE9684B1E92634900437A67 /* CameraEngine.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -41,6 +59,7 @@
BB3B05C61D8C242900165E2C = {
isa = PBXGroup;
children = (
2FE9684A1E92634900437A67 /* CameraEngine.framework */,
BB3B05D11D8C242A00165E2C /* CameraEngineExample */,
BB3B05D01D8C242A00165E2C /* Products */,
BB9DFAD41D8C4D95001E7B35 /* Frameworks */,
Expand Down Expand Up @@ -86,6 +105,7 @@
BB3B05CB1D8C242A00165E2C /* Sources */,
BB3B05CC1D8C242A00165E2C /* Frameworks */,
BB3B05CD1D8C242A00165E2C /* Resources */,
2FE9684D1E92634900437A67 /* Embed Frameworks */,
);
buildRules = (
);
Expand All @@ -108,7 +128,7 @@
TargetAttributes = {
BB3B05CE1D8C242A00165E2C = {
CreatedOnToolsVersion = 8.0;
DevelopmentTeam = 44Q2J6374R;
DevelopmentTeam = 2R66XDN32F;
ProvisioningStyle = Automatic;
};
};
Expand Down Expand Up @@ -273,7 +293,8 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 44Q2J6374R;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 2R66XDN32F;
INFOPLIST_FILE = CameraEngineExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.remirobert.CameraEngineExample;
Expand All @@ -286,7 +307,8 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 44Q2J6374R;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
DEVELOPMENT_TEAM = 2R66XDN32F;
INFOPLIST_FILE = CameraEngineExample/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.remirobert.CameraEngineExample;
Expand Down

0 comments on commit 61812a4

Please sign in to comment.