Skip to content

Commit

Permalink
tweak main thread util
Browse files Browse the repository at this point in the history
  • Loading branch information
dnpp73 committed Oct 18, 2018
1 parent 119ca27 commit 33b9690
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
8 changes: 4 additions & 4 deletions SimpleCamera.xcodeproj/project.pbxproj
Expand Up @@ -12,7 +12,7 @@
EA5C8B7C1EA73A6A00E5E016 /* CIContext+Workaround.m in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B7A1EA73A6A00E5E016 /* CIContext+Workaround.m */; };
EA5C8B821EA73B8400E5E016 /* CameraUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B7E1EA73B8400E5E016 /* CameraUtil.swift */; };
EA5C8B831EA73B8400E5E016 /* ImageUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B7F1EA73B8400E5E016 /* ImageUtil.swift */; };
EA5C8B841EA73B8400E5E016 /* OnMainThread.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B801EA73B8400E5E016 /* OnMainThread.swift */; };
EA5C8B841EA73B8400E5E016 /* MainThread.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B801EA73B8400E5E016 /* MainThread.swift */; };
EA5C8B851EA73B8400E5E016 /* UIImageExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B811EA73B8400E5E016 /* UIImageExtension.swift */; };
EA5C8B8A1EA73BCC00E5E016 /* OrientationDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B871EA73BCC00E5E016 /* OrientationDetector.swift */; };
EA5C8B8B1EA73BCC00E5E016 /* OrientationDetectorInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA5C8B881EA73BCC00E5E016 /* OrientationDetectorInterface.swift */; };
Expand All @@ -39,7 +39,7 @@
EA5C8B7A1EA73A6A00E5E016 /* CIContext+Workaround.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CIContext+Workaround.m"; sourceTree = "<group>"; };
EA5C8B7E1EA73B8400E5E016 /* CameraUtil.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CameraUtil.swift; sourceTree = "<group>"; };
EA5C8B7F1EA73B8400E5E016 /* ImageUtil.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageUtil.swift; sourceTree = "<group>"; };
EA5C8B801EA73B8400E5E016 /* OnMainThread.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnMainThread.swift; sourceTree = "<group>"; };
EA5C8B801EA73B8400E5E016 /* MainThread.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainThread.swift; sourceTree = "<group>"; };
EA5C8B811EA73B8400E5E016 /* UIImageExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageExtension.swift; sourceTree = "<group>"; };
EA5C8B871EA73BCC00E5E016 /* OrientationDetector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrientationDetector.swift; sourceTree = "<group>"; };
EA5C8B881EA73BCC00E5E016 /* OrientationDetectorInterface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrientationDetectorInterface.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -118,7 +118,7 @@
EA5C8B7D1EA73B6600E5E016 /* Util */ = {
isa = PBXGroup;
children = (
EA5C8B801EA73B8400E5E016 /* OnMainThread.swift */,
EA5C8B801EA73B8400E5E016 /* MainThread.swift */,
EA5C8B7E1EA73B8400E5E016 /* CameraUtil.swift */,
EA5C8B7F1EA73B8400E5E016 /* ImageUtil.swift */,
EA5C8B811EA73B8400E5E016 /* UIImageExtension.swift */,
Expand Down Expand Up @@ -301,7 +301,7 @@
EA5C8B851EA73B8400E5E016 /* UIImageExtension.swift in Sources */,
EA5C8B8A1EA73BCC00E5E016 /* OrientationDetector.swift in Sources */,
EA5C8B8C1EA73BCC00E5E016 /* OrientationExtension.swift in Sources */,
EA5C8B841EA73B8400E5E016 /* OnMainThread.swift in Sources */,
EA5C8B841EA73B8400E5E016 /* MainThread.swift in Sources */,
EA5C8BA11EA73CDF00E5E016 /* CameraFinderView.swift in Sources */,
EA5C8BA31EA73CDF00E5E016 /* CameraFinderViewInterface.swift in Sources */,
EA5C8B9A1EA73C9200E5E016 /* GridView.swift in Sources */,
Expand Down
17 changes: 17 additions & 0 deletions SimpleCamera/MainThread.swift
@@ -0,0 +1,17 @@
import Foundation

internal func onMainThreadAsync(execute work: @escaping @convention(block) () -> Swift.Void) {
if Thread.isMainThread {
work()
} else {
DispatchQueue.main.async(execute: work)
}
}

internal func onMainThreadSync(execute work: () -> Swift.Void) {
if Thread.isMainThread {
work()
} else {
DispatchQueue.main.sync(execute: work)
}
}
9 changes: 0 additions & 9 deletions SimpleCamera/OnMainThread.swift

This file was deleted.

34 changes: 17 additions & 17 deletions SimpleCamera/SimpleCamera.swift
Expand Up @@ -242,13 +242,13 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {

self.imageOutput.captureStillImageAsynchronously(from: captureImageConnection) { (imageDataBuffer, error) -> Void in
guard let imageDataBuffer = imageDataBuffer, let data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataBuffer) else {
onMainThread {
onMainThreadAsync {
completion(nil, nil)
}
return
}
guard let rawImage = UIImage(data: data) else {
onMainThread {
onMainThreadAsync {
completion(nil, nil)
}
return
Expand All @@ -259,7 +259,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
scaledImage = rawImage
} else {
guard let c = CIImage(image: rawImage), let i = createUIImage(from: c, limitSize: self.captureLimitSize, imageOrientation: rawImage.imageOrientation) else {
onMainThread {
onMainThreadAsync {
completion(nil, nil)
}
return
Expand All @@ -270,7 +270,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
let metadata = CMCopyDictionaryOfAttachments(allocator: nil, target: imageDataBuffer, attachmentMode: kCMAttachmentMode_ShouldPropagate) as? [String: Any]
let mirrored = self.isMirroredImageIfFrontCamera && captureImageConnection.isFrontCameraDevice
let image = mirrored ? scaledImage.mirrored : scaledImage
onMainThread {
onMainThreadAsync {
completion(image, metadata)
}
}
Expand Down Expand Up @@ -506,7 +506,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
device.exposureMode = exposureMode
}
device.isSubjectAreaChangeMonitoringEnabled = true
onMainThread {
onMainThreadAsync {
for observer in self.observers.allObjects {
observer.simpleCameraDidResetFocusAndExposure(simpleCamera: self)
}
Expand Down Expand Up @@ -551,7 +551,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {

if switchSucceed {
// 内部で sessionQueue.async してるので外に出しておきたい
onMainThread {
onMainThreadAsync {
for observer in self.observers.allObjects {
observer.simpleCameraDidSwitchCameraInput(simpleCamera: self)
}
Expand Down Expand Up @@ -810,7 +810,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
guard let isRunning = changes.newValue else {
return
}
onMainThread {
onMainThreadAsync {
self.isRunningForObserve = isRunning
}
}))
Expand Down Expand Up @@ -856,47 +856,47 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
guard let focusPointOfInterest = changes.newValue else {
return
}
onMainThread {
onMainThreadAsync {
self.focusPointOfInterestForObserve = focusPointOfInterest
}
}))
observations.append(backCameraVideoInput?.device.observe(\.focusPointOfInterest, options: .new, changeHandler: { (device, changes) in
guard let focusPointOfInterest = changes.newValue else {
return
}
onMainThread {
onMainThreadAsync {
self.focusPointOfInterestForObserve = focusPointOfInterest
}
}))
observations.append(frontCameraVideoInput?.device.observe(\.exposurePointOfInterest, options: .new, changeHandler: { (device, changes) in
guard let exposurePointOfInterest = changes.newValue else {
return
}
onMainThread {
onMainThreadAsync {
self.exposurePointOfInterestForObserve = exposurePointOfInterest
}
}))
observations.append(backCameraVideoInput?.device.observe(\.exposurePointOfInterest, options: .new, changeHandler: { (device, changes) in
guard let exposurePointOfInterest = changes.newValue else {
return
}
onMainThread {
onMainThreadAsync {
self.exposurePointOfInterestForObserve = exposurePointOfInterest
}
}))
observations.append(frontCameraVideoInput?.device.observe(\.videoZoomFactor, options: .new, changeHandler: { (device, changes) in
guard let videoZoomFactor = changes.newValue else {
return
}
onMainThread {
onMainThreadAsync {
self.zoomFactorForObserve = videoZoomFactor
}
}))
observations.append(backCameraVideoInput?.device.observe(\.videoZoomFactor, options: .new, changeHandler: { (device, changes) in
guard let videoZoomFactor = changes.newValue else {
return
}
onMainThread {
onMainThreadAsync {
self.zoomFactorForObserve = videoZoomFactor
}
}))
Expand Down Expand Up @@ -928,7 +928,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
return
}
let error = AVError(_nsError: errorValue)
onMainThread {
onMainThreadAsync {
for observer in self.observers.allObjects {
observer.simpleCameraSessionRuntimeError(simpleCamera: self, error: error)
}
Expand All @@ -945,7 +945,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
guard let userInfoValue = notification.userInfo?[AVCaptureSessionInterruptionReasonKey] as AnyObject?, let reasonIntegerValue = userInfoValue.integerValue, let reason = AVCaptureSession.InterruptionReason(rawValue: reasonIntegerValue) else {
return
}
onMainThread {
onMainThreadAsync {
for observer in self.observers.allObjects {
observer.simpleCameraSessionWasInterrupted(simpleCamera: self, reason: reason)
}
Expand All @@ -954,7 +954,7 @@ public final class SimpleCamera: NSObject, SimpleCameraInterface {
}

@objc private func sessionInterruptionEnded(notification: Notification) {
onMainThread {
onMainThreadAsync {
for observer in self.observers.allObjects {
observer.simpleCameraSessionInterruptionEnded(simpleCamera: self)
}
Expand Down Expand Up @@ -1058,7 +1058,7 @@ extension SimpleCamera: AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureA
let image = createUIImage(from: sampleBuffer, limitSize: captureLimitSize, imageOrientation: preferredUIImageOrientationForVideoOutput)
let metadata = CMCopyDictionaryOfAttachments(allocator: nil, target: sampleBuffer, attachmentMode: kCMAttachmentMode_ShouldPropagate) as? [String: Any]
isSilentCapturingImage = false
onMainThread {
onMainThreadAsync {
silentCaptureImageCompletion(image, metadata)
}
}
Expand Down

0 comments on commit 33b9690

Please sign in to comment.