Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
22717582
Commit
22717582
authored
Jul 13, 2016
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
updated comments to CaptureSession
parent
17b67d99
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
215 additions
and
252 deletions
+215
-252
Sources/iOS/CapturePreview.swift
+7
-3
Sources/iOS/CaptureSession.swift
+188
-223
Sources/iOS/CaptureView.swift
+5
-5
Sources/iOS/ControlView.swift
+1
-1
Sources/iOS/Grid.swift
+2
-3
Sources/iOS/InterimSpace.swift
+1
-1
Sources/iOS/MaterialCollectionReusableView.swift
+2
-3
Sources/iOS/MaterialCollectionView.swift
+2
-3
Sources/iOS/MaterialCollectionViewCell.swift
+2
-3
Sources/iOS/MaterialCollectionViewLayout.swift
+2
-3
Sources/iOS/Menu.swift
+1
-1
Sources/iOS/NavigationBar.swift
+2
-3
No files found.
Sources/iOS/CapturePreview.swift
View file @
22717582
...
...
@@ -53,9 +53,13 @@ public class CapturePreview : MaterialView {
return
(
layer
as!
AVCaptureVideoPreviewLayer
)
.
pointForCaptureDevicePoint
(
ofInterest
:
point
)
}
/**
:name: prepareView
*/
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
public
override
func
prepareView
()
{
super
.
prepareView
()
preparePreviewLayer
()
...
...
Sources/iOS/CaptureSession.swift
View file @
22717582
...
...
@@ -34,52 +34,53 @@ import AVFoundation
private
var
CaptureSessionAdjustingExposureContext
:
UInt8
=
1
public
enum
CaptureSessionPreset
{
case
P
resetPhoto
case
P
resetHigh
case
P
resetMedium
case
P
resetLow
case
P
reset352x288
case
P
reset640x480
case
P
reset1280x720
case
P
reset1920x1080
case
P
reset3840x2160
case
P
resetiFrame960x540
case
P
resetiFrame1280x720
case
P
resetInputPriority
case
p
resetPhoto
case
p
resetHigh
case
p
resetMedium
case
p
resetLow
case
p
reset352x288
case
p
reset640x480
case
p
reset1280x720
case
p
reset1920x1080
case
p
reset3840x2160
case
p
resetiFrame960x540
case
p
resetiFrame1280x720
case
p
resetInputPriority
}
/**
:name: CaptureSessionPresetToString
*/
Converts a given CaptureSessionPreset to a String value.
- Parameter preset: A CaptureSessionPreset to convert.
*/
public
func
CaptureSessionPresetToString
(
preset
:
CaptureSessionPreset
)
->
String
{
switch
preset
{
case
.
P
resetPhoto
:
case
.
p
resetPhoto
:
return
AVCaptureSessionPresetPhoto
case
.
P
resetHigh
:
case
.
p
resetHigh
:
return
AVCaptureSessionPresetHigh
case
.
P
resetMedium
:
case
.
p
resetMedium
:
return
AVCaptureSessionPresetMedium
case
.
P
resetLow
:
case
.
p
resetLow
:
return
AVCaptureSessionPresetLow
case
.
P
reset352x288
:
case
.
p
reset352x288
:
return
AVCaptureSessionPreset352x288
case
.
P
reset640x480
:
case
.
p
reset640x480
:
return
AVCaptureSessionPreset640x480
case
.
P
reset1280x720
:
case
.
p
reset1280x720
:
return
AVCaptureSessionPreset1280x720
case
.
P
reset1920x1080
:
case
.
p
reset1920x1080
:
return
AVCaptureSessionPreset1920x1080
case
.
P
reset3840x2160
:
case
.
p
reset3840x2160
:
if
#available(iOS 9.0, *)
{
return
AVCaptureSessionPreset3840x2160
}
else
{
return
AVCaptureSessionPresetHigh
}
case
.
P
resetiFrame960x540
:
case
.
p
resetiFrame960x540
:
return
AVCaptureSessionPresetiFrame960x540
case
.
P
resetiFrame1280x720
:
case
.
p
resetiFrame1280x720
:
return
AVCaptureSessionPresetiFrame1280x720
case
.
P
resetInputPriority
:
case
.
p
resetInputPriority
:
return
AVCaptureSessionPresetInputPriority
}
}
...
...
@@ -116,7 +117,7 @@ public protocol CaptureSessionDelegate {
- Parameter image: An image that has been captured.
*/
@objc
optional
func
captureStillImageAsynchronously
(
captureSession
:
CaptureSession
,
image
:
UIImage
)
optional
func
captureS
essionS
tillImageAsynchronously
(
captureSession
:
CaptureSession
,
image
:
UIImage
)
/**
A delegation method that is fired when capturing an image asynchronously has failed.
...
...
@@ -124,161 +125,136 @@ public protocol CaptureSessionDelegate {
- Parameter error: A NSError corresponding to the error.
*/
@objc
optional
func
captureStillImageAsynchronouslyFailedWithError
(
captureSession
:
CaptureSession
,
error
:
NSError
)
optional
func
captureS
essionS
tillImageAsynchronouslyFailedWithError
(
captureSession
:
CaptureSession
,
error
:
NSError
)
/**
:name: captureCreateMovieFileFailedWithError
*/
/**
A delegation method that is fired when creating a movie file has failed.
- Parameter captureSession: A reference to the calling CaptureSession.
- Parameter error: A NSError corresponding to the error.
*/
@objc
optional
func
captureCreateMovieFileFailedWithError
(
captureSession
:
CaptureSession
,
error
:
NSError
)
optional
func
capture
Session
CreateMovieFileFailedWithError
(
captureSession
:
CaptureSession
,
error
:
NSError
)
/**
:name: captureMovieFailedWithError
*/
/**
A delegation method that is fired when capturing a movie has failed.
- Parameter captureSession: A reference to the calling CaptureSession.
- Parameter error: A NSError corresponding to the error.
*/
@objc
optional
func
captureMovieFailedWithError
(
captureSession
:
CaptureSession
,
error
:
NSError
)
optional
func
capture
Session
MovieFailedWithError
(
captureSession
:
CaptureSession
,
error
:
NSError
)
/**
:name: captureDidStartRecordingToOutputFileAtURL
*/
/**
A delegation method that is fired when a session started recording and writing
to a file.
- Parameter captureSession: A reference to the calling CaptureSession.
- Parameter captureOut: An AVCaptureFileOutput.
- Parameter fileURL: A file URL.
- Parameter fromConnections: An array of AnyObjects.
*/
@objc
optional
func
captureDidStartRecordingToOutputFileAtURL
(
captureSession
:
CaptureSession
,
captureOutput
:
AVCaptureFileOutput
,
fileURL
:
NSURL
,
fromConnections
connections
:
[
AnyObject
])
optional
func
capture
Session
DidStartRecordingToOutputFileAtURL
(
captureSession
:
CaptureSession
,
captureOutput
:
AVCaptureFileOutput
,
fileURL
:
NSURL
,
fromConnections
connections
:
[
AnyObject
])
/**
:name: captureDidFinishRecordingToOutputFileAtURL
*/
/**
A delegation method that is fired when a session finished recording and writing
to a file.
- Parameter captureSession: A reference to the calling CaptureSession.
- Parameter captureOut: An AVCaptureFileOutput.
- Parameter fileURL: A file URL.
- Parameter fromConnections: An array of AnyObjects.
- Parameter error: A NSError corresponding to an error.
*/
@objc
optional
func
captureDidFinishRecordingToOutputFileAtURL
(
captureSession
:
CaptureSession
,
captureOutput
:
AVCaptureFileOutput
,
outputFileURL
:
NSURL
,
fromConnections
connections
:
[
AnyObject
],
error
:
NSError
!
)
optional
func
capture
Session
DidFinishRecordingToOutputFileAtURL
(
captureSession
:
CaptureSession
,
captureOutput
:
AVCaptureFileOutput
,
outputFileURL
:
NSURL
,
fromConnections
connections
:
[
AnyObject
],
error
:
NSError
!
)
}
@objc(CaptureSession)
public
class
CaptureSession
:
NSObject
,
AVCaptureFileOutputRecordingDelegate
{
/**
:name: sessionQueue
*/
/// A reference to the session DispatchQueue.
private
var
sessionQueue
:
DispatchQueue
!
/**
:name: activeVideoInput
*/
/// A reference to the active video input.
private
var
activeVideoInput
:
AVCaptureDeviceInput
?
/**
:name: activeAudioInput
*/
/// A reference to the active audio input.
private
var
activeAudioInput
:
AVCaptureDeviceInput
?
/**
:name: imageOutput
*/
private
lazy
var
imageOutput
:
AVCaptureStillImageOutput
=
AVCaptureStillImageOutput
()
/// A reference to the image output.
private
var
imageOutput
:
AVCaptureStillImageOutput
!
/**
:name: movieOutput
*/
private
lazy
var
movieOutput
:
AVCaptureMovieFileOutput
=
AVCaptureMovieFileOutput
()
/// A reference to the movie output.
private
var
movieOutput
:
AVCaptureMovieFileOutput
!
/**
:name: movieOutputURL
*/
/// A reference to the movie output URL.
private
var
movieOutputURL
:
NSURL
?
/**
:name: session
*/
internal
lazy
var
session
:
AVCaptureSession
=
AVCaptureSession
()
/// A reference to the AVCaptureSession.
internal
var
session
:
AVCaptureSession
!
/**
:name: isRunning
*/
public
private(set)
lazy
var
isRunning
:
Bool
=
false
/// A boolean indicating if the session is running.
public
private(set)
var
isRunning
:
Bool
=
false
/**
:name: isRecording
*/
public
private(set)
lazy
var
isRecording
:
Bool
=
false
/// A boolean indicating if the session is recording.
public
private(set)
var
isRecording
:
Bool
=
false
/**
:name: recordedDuration
*/
public
var
recordedDuration
:
CMTime
{
/// A reference to the recorded time duration.
public
var
recordedDuration
:
CMTime
{
return
movieOutput
.
recordedDuration
}
/**
:name: activeCamera
*/
/// An optional reference to the active camera if one exists.
public
var
activeCamera
:
AVCaptureDevice
?
{
return
activeVideoInput
?
.
device
}
/**
:name: inactiveCamera
*/
/// An optional reference to the inactive camera if one exists.
public
var
inactiveCamera
:
AVCaptureDevice
?
{
var
device
:
AVCaptureDevice
?
if
1
<
cameraCount
{
if
activeCamera
?
.
position
==
.
back
{
device
=
camera
WithPosition
(
position
:
.
front
)
device
=
camera
(
position
:
.
front
)
}
else
{
device
=
camera
WithPosition
(
position
:
.
back
)
device
=
camera
(
position
:
.
back
)
}
}
return
device
}
/**
:name: cameraCount
*/
/// Available number of cameras.
public
var
cameraCount
:
Int
{
return
AVCaptureDevice
.
devices
(
withMediaType
:
AVMediaTypeVideo
)
.
count
}
/**
:name: canSwitchCameras
*/
/// A boolean indicating whether the camera can switch to another.
public
var
canSwitchCameras
:
Bool
{
return
1
<
cameraCount
}
/**
:name: caneraSupportsTapToFocus
*/
public
var
cameraSupportsTapToFocus
:
Bool
{
/// A booealn indicating whether the camrea supports focus.
public
var
isFocusPointOfInterestSupported
:
Bool
{
return
nil
==
activeCamera
?
false
:
activeCamera
!.
isFocusPointOfInterestSupported
}
/**
:name: cameraSupportsTapToExpose
*/
public
var
cameraSupportsTapToExpose
:
Bool
{
/// A booealn indicating whether the camrea supports exposure.
public
var
isExposurePointOfInterestSupported
:
Bool
{
return
nil
==
activeCamera
?
false
:
activeCamera
!.
isExposurePointOfInterestSupported
}
/**
:name: cameraHasFlash
*/
public
var
cameraHasFlash
:
Bool
{
/// A boolean indicating if the active camera has flash.
public
var
hasFlash
:
Bool
{
return
nil
==
activeCamera
?
false
:
activeCamera
!.
hasFlash
}
/**
:name: cameraHasTorch
*/
public
var
cameraHasTorch
:
Bool
{
/// A boolean indicating if the active camera has a torch.
public
var
hasTorch
:
Bool
{
return
nil
==
activeCamera
?
false
:
activeCamera
!.
hasTorch
}
/**
:name: cameraPosition
*/
public
var
cameraPosition
:
AVCaptureDevicePosition
?
{
/// A reference to the active camera position if the active camera exists.
public
var
position
:
AVCaptureDevicePosition
?
{
return
activeCamera
?
.
position
}
/**
:name: focusMode
*/
/// A reference to the focusMode.
public
var
focusMode
:
AVCaptureFocusMode
{
get
{
return
activeCamera
!.
focusMode
...
...
@@ -302,14 +278,12 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
userInfo
[
NSUnderlyingErrorKey
]
=
error
}
if
let
e
:
NSError
=
error
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
}
/**
:name: flashMode
*/
/// A reference to the flashMode.
public
var
flashMode
:
AVCaptureFlashMode
{
get
{
return
activeCamera
!.
flashMode
...
...
@@ -333,14 +307,12 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
userInfo
[
NSUnderlyingErrorKey
]
=
error
}
if
let
e
:
NSError
=
error
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
}
/**
:name: torchMode
*/
/// A reference to the torchMode.
public
var
torchMode
:
AVCaptureTorchMode
{
get
{
return
activeCamera
!.
torchMode
...
...
@@ -364,15 +336,15 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
userInfo
[
NSUnderlyingErrorKey
]
=
error
}
if
let
e
:
NSError
=
error
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
}
/// The session quality preset.
public
var
sessionP
reset
:
CaptureSessionPreset
{
public
var
p
reset
:
CaptureSessionPreset
{
didSet
{
session
.
sessionPreset
=
CaptureSessionPresetToString
(
preset
:
sessionP
reset
)
session
.
sessionPreset
=
CaptureSessionPresetToString
(
preset
:
p
reset
)
}
}
...
...
@@ -397,9 +369,13 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
/// Initializer.
public
override
init
()
{
sessionPreset
=
.
P
resetHigh
preset
=
.
p
resetHigh
super
.
init
()
prepareSession
()
prepareActiveVideoInput
()
prepareActiveAudioInput
()
prepareImageOutput
()
prepareMovieOutput
()
}
/// Starts the session.
...
...
@@ -424,8 +400,8 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
public
func
switchCameras
()
{
if
canSwitchCameras
{
do
{
if
let
v
:
AVCaptureDevicePosition
=
cameraP
osition
{
delegate
?
.
captureSessionWillSwitchCameras
?(
capture
:
self
,
position
:
v
)
if
let
v
:
AVCaptureDevicePosition
=
p
osition
{
delegate
?
.
captureSessionWillSwitchCameras
?(
capture
Session
:
self
,
position
:
v
)
let
videoInput
:
AVCaptureDeviceInput
?
=
try
AVCaptureDeviceInput
(
device
:
inactiveCamera
!
)
session
.
beginConfiguration
()
session
.
removeInput
(
activeVideoInput
)
...
...
@@ -437,48 +413,57 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
session
.
addInput
(
activeVideoInput
)
}
session
.
commitConfiguration
()
delegate
?
.
captureSessionDidSwitchCameras
?(
capture
:
self
,
position
:
cameraP
osition
!
)
delegate
?
.
captureSessionDidSwitchCameras
?(
capture
Session
:
self
,
position
:
p
osition
!
)
}
}
catch
let
e
as
NSError
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
}
/**
:name: isFocusModeSupported
*/
Checks if a given focus mode is supported.
- Parameter focusMode: An AVCaptureFocusMode.
- Returns: A boolean of the result, true if supported, false otherwise.
*/
public
func
isFocusModeSupported
(
focusMode
:
AVCaptureFocusMode
)
->
Bool
{
return
activeCamera
!.
isFocusModeSupported
(
focusMode
)
}
/**
:name: isExposureModeSupported
*/
public
func
isExposureModeSupported
(
exposureMode
:
AVCaptureExposureMode
)
->
Bool
{
/**
Checks if a given exposure mode is supported.
- Parameter exposureMode: An AVCaptureExposureMode.
- Returns: A boolean of the result, true if supported, false otherwise.
*/
public
func
isExposureModeSupported
(
exposureMode
:
AVCaptureExposureMode
)
->
Bool
{
return
activeCamera
!.
isExposureModeSupported
(
exposureMode
)
}
/**
:name: isFlashModeSupported
*/
public
func
isFlashModeSupported
(
flashMode
:
AVCaptureFlashMode
)
->
Bool
{
/**
Checks if a given flash mode is supported.
- Parameter flashMode: An AVCaptureFlashMode.
- Returns: A boolean of the result, true if supported, false otherwise.
*/
public
func
isFlashModeSupported
(
flashMode
:
AVCaptureFlashMode
)
->
Bool
{
return
activeCamera
!.
isFlashModeSupported
(
flashMode
)
}
/**
:name: isTorchModeSupported
*/
public
func
isTorchModeSupported
(
torchMode
:
AVCaptureTorchMode
)
->
Bool
{
/**
Checks if a given torch mode is supported.
- Parameter torchMode: An AVCaptureTorchMode.
- Returns: A boolean of the result, true if supported, false otherwise.
*/
public
func
isTorchModeSupported
(
torchMode
:
AVCaptureTorchMode
)
->
Bool
{
return
activeCamera
!.
isTorchModeSupported
(
torchMode
)
}
/**
:name: focusAtPoint
*/
public
func
focusAtPoint
(
point
:
CGPoint
)
{
Focuses the camera at a given point.
- Parameter at: A CGPoint to focus at.
*/
public
func
focus
(
at
point
:
CGPoint
)
{
var
error
:
NSError
?
if
cameraSupportsTapToFocus
&&
isFocusModeSupported
(
focusMode
:
.
autoFocus
)
{
if
isFocusPointOfInterestSupported
&&
isFocusModeSupported
(
focusMode
:
.
autoFocus
)
{
do
{
let
device
:
AVCaptureDevice
=
activeCamera
!
try
device
.
lockForConfiguration
()
...
...
@@ -490,22 +475,23 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
else
{
var
userInfo
:
Dictionary
<
String
,
AnyObject
>
=
Dictionary
<
String
,
AnyObject
>
()
userInfo
[
NSLocalizedDescriptionKey
]
=
"[Material Error: Unsupported focus
AtPoint
.]"
userInfo
[
NSLocalizedFailureReasonErrorKey
]
=
"[Material Error: Unsupported focus
AtPoint
.]"
userInfo
[
NSLocalizedDescriptionKey
]
=
"[Material Error: Unsupported focus.]"
userInfo
[
NSLocalizedFailureReasonErrorKey
]
=
"[Material Error: Unsupported focus.]"
error
=
NSError
(
domain
:
"io.cosmicmind.Material.CaptureView"
,
code
:
0004
,
userInfo
:
userInfo
)
userInfo
[
NSUnderlyingErrorKey
]
=
error
}
if
let
e
:
NSError
=
error
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
/**
:name: exposeAtPoint
*/
public
func
exposeAtPoint
(
point
:
CGPoint
)
{
/**
Exposes the camera at a given point.
- Parameter at: A CGPoint to expose at.
*/
public
func
expose
(
at
point
:
CGPoint
)
{
var
error
:
NSError
?
if
cameraSupportsTapToExpose
&&
isExposureModeSupported
(
exposureMode
:
.
continuousAutoExposure
)
{
if
isExposurePointOfInterestSupported
&&
isExposureModeSupported
(
exposureMode
:
.
continuousAutoExposure
)
{
do
{
let
device
:
AVCaptureDevice
=
activeCamera
!
try
device
.
lockForConfiguration
()
...
...
@@ -520,19 +506,16 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
else
{
var
userInfo
:
Dictionary
<
String
,
AnyObject
>
=
Dictionary
<
String
,
AnyObject
>
()
userInfo
[
NSLocalizedDescriptionKey
]
=
"[Material Error: Unsupported expose
AtPoint
.]"
userInfo
[
NSLocalizedFailureReasonErrorKey
]
=
"[Material Error: Unsupported expose
AtPoint
.]"
userInfo
[
NSLocalizedDescriptionKey
]
=
"[Material Error: Unsupported expose.]"
userInfo
[
NSLocalizedFailureReasonErrorKey
]
=
"[Material Error: Unsupported expose.]"
error
=
NSError
(
domain
:
"io.cosmicmind.Material.CaptureView"
,
code
:
0005
,
userInfo
:
userInfo
)
userInfo
[
NSUnderlyingErrorKey
]
=
error
}
if
let
e
:
NSError
=
error
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
/**
:name: observeValueForKeyPath
*/
public
override
func
observeValue
(
forKeyPath
keyPath
:
String
?,
of
object
:
AnyObject
?,
change
:
[
NSKeyValueChangeKey
:
AnyObject
]?,
context
:
UnsafeMutablePointer
<
Void
>
?)
{
if
context
==
&
CaptureSessionAdjustingExposureContext
{
let
device
:
AVCaptureDevice
=
object
as!
AVCaptureDevice
...
...
@@ -544,7 +527,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
device
.
exposureMode
=
.
locked
device
.
unlockForConfiguration
()
}
catch
let
e
as
NSError
{
self
.
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
self
.
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
}
...
...
@@ -554,32 +537,32 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
}
/**
:name: resetFocusAndExposureModes
*/
public
func
resetFocusAndExposureModes
()
{
Resets the camera focus and exposure.
- Parameter focus: A boolean indicating to reset the focus.
- Parameter exposure: A boolean indicating to reset the exposure.
*/
public
func
reset
(
focus
:
Bool
=
true
,
exposure
:
Bool
=
true
)
{
let
device
:
AVCaptureDevice
=
activeCamera
!
let
canResetFocus
:
Bool
=
device
.
isFocusPointOfInterestSupported
&&
device
.
isFocusModeSupported
(
.
continuousAutoFocus
)
let
canResetExposure
:
Bool
=
device
.
isExposurePointOfInterestSupported
&&
device
.
isExposureModeSupported
(
.
continuousAutoExposure
)
let
centerPoint
:
CGPoint
=
CGPoint
(
x
:
0.5
,
y
:
0.5
)
do
{
try
device
.
lockForConfiguration
()
if
canResetFocus
{
if
canResetFocus
&&
focus
{
device
.
focusMode
=
.
continuousAutoFocus
device
.
focusPointOfInterest
=
centerPoint
}
if
canResetExposure
{
if
canResetExposure
&&
exposure
{
device
.
exposureMode
=
.
continuousAutoExposure
device
.
exposurePointOfInterest
=
centerPoint
}
device
.
unlockForConfiguration
()
}
catch
let
e
as
NSError
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
/**
:name: captureStillImage
*/
/// Captures a still image.
public
func
captureStillImage
()
{
sessionQueue
.
async
()
{
[
weak
self
]
in
if
let
s
:
CaptureSession
=
self
{
...
...
@@ -592,7 +575,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
let
data
:
NSData
=
AVCaptureStillImageOutput
.
jpegStillImageNSDataRepresentation
(
sampleBuffer
)
if
let
image1
:
UIImage
=
UIImage
(
data
:
data
as
Data
)
{
if
let
image2
:
UIImage
=
s
.
adjustOrientationForImage
(
image
:
image1
)
{
s
.
delegate
?
.
captureS
tillImageAsynchronously
?(
capture
:
s
,
image
:
image2
)
s
.
delegate
?
.
captureS
essionStillImageAsynchronously
?(
captureSession
:
s
,
image
:
image2
)
}
else
{
var
userInfo
:
Dictionary
<
String
,
AnyObject
>
=
Dictionary
<
String
,
AnyObject
>
()
userInfo
[
NSLocalizedDescriptionKey
]
=
"[Material Error: Cannot fix image orientation.]"
...
...
@@ -610,7 +593,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
}
if
let
e
:
NSError
=
captureError
{
s
.
delegate
?
.
captureS
tillImageAsynchronouslyFailedWithError
?(
capture
:
s
,
error
:
e
)
s
.
delegate
?
.
captureS
essionStillImageAsynchronouslyFailedWithError
?(
captureSession
:
s
,
error
:
e
)
}
}
}
...
...
@@ -619,9 +602,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
/**
:name: startRecording
*/
/// Starts recording.
public
func
startRecording
()
{
if
!
isRecording
{
sessionQueue
.
async
()
{
[
weak
self
]
in
...
...
@@ -637,7 +618,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
v
.
isSmoothAutoFocusEnabled
=
true
v
.
unlockForConfiguration
()
}
catch
let
e
as
NSError
{
s
.
delegate
?
.
captureSessionFailedWithError
?(
capture
:
s
,
error
:
e
)
s
.
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
s
,
error
:
e
)
}
}
...
...
@@ -651,29 +632,21 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
/**
:name: stopRecording
*/
/// Stops recording.
public
func
stopRecording
()
{
if
isRecording
{
movieOutput
.
stopRecording
()
}
}
/**
:name: captureOutput
*/
public
func
capture
(
_
captureOutput
:
AVCaptureFileOutput
!
,
didStartRecordingToOutputFileAt
fileURL
:
URL
!
,
fromConnections
connections
:
[
AnyObject
]
!
)
{
public
func
capture
(
_
captureOutput
:
AVCaptureFileOutput
!
,
didStartRecordingToOutputFileAt
fileURL
:
URL
!
,
fromConnections
connections
:
[
AnyObject
]
!
)
{
isRecording
=
true
delegate
?
.
capture
DidStartRecordingToOutputFileAtURL
?(
capture
:
self
,
captureOutput
:
captureOutput
,
fileURL
:
fileURL
,
fromConnections
:
connections
)
delegate
?
.
capture
SessionDidStartRecordingToOutputFileAtURL
?(
captureSession
:
self
,
captureOutput
:
captureOutput
,
fileURL
:
fileURL
,
fromConnections
:
connections
)
}
/**
:name: captureOutput
*/
public
func
capture
(
_
captureOutput
:
AVCaptureFileOutput
!
,
didFinishRecordingToOutputFileAt
outputFileURL
:
URL
!
,
fromConnections
connections
:
[
AnyObject
]
!
,
error
:
NSError
!
)
{
public
func
capture
(
_
captureOutput
:
AVCaptureFileOutput
!
,
didFinishRecordingToOutputFileAt
outputFileURL
:
URL
!
,
fromConnections
connections
:
[
AnyObject
]
!
,
error
:
NSError
!
)
{
isRecording
=
false
delegate
?
.
capture
DidFinishRecordingToOutputFileAtURL
?(
capture
:
self
,
captureOutput
:
captureOutput
,
outputFileURL
:
outputFileURL
,
fromConnections
:
connections
,
error
:
error
)
delegate
?
.
capture
SessionDidFinishRecordingToOutputFileAtURL
?(
captureSession
:
self
,
captureOutput
:
captureOutput
,
outputFileURL
:
outputFileURL
,
fromConnections
:
connections
,
error
:
error
)
}
/// Prepares the sessionQueue.
...
...
@@ -681,67 +654,58 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
sessionQueue
=
DispatchQueue
(
label
:
"io.cosmicmind.Material.CaptureSession"
,
attributes
:
.
serial
,
target
:
nil
)
}
/**
:name: prepareSession
*/
/// Prepares the session.
private
func
prepareSession
()
{
prepareVideoInput
()
prepareAudioInput
()
prepareImageOutput
()
prepareMovieOutput
()
session
=
AVCaptureSession
()
}
/**
:name: prepareVideoInput
*/
private
func
prepareVideoInput
()
{
/// Prepares the activeVideoInput.
private
func
prepareActiveVideoInput
()
{
do
{
activeVideoInput
=
try
AVCaptureDeviceInput
(
device
:
AVCaptureDevice
.
defaultDevice
(
withMediaType
:
AVMediaTypeVideo
))
if
session
.
canAddInput
(
activeVideoInput
)
{
session
.
addInput
(
activeVideoInput
)
}
}
catch
let
e
as
NSError
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
/**
:name: prepareAudioInput
*/
private
func
prepareAudioInput
()
{
/// Prepares the activeAudioInput.
private
func
prepareActiveAudioInput
()
{
do
{
activeAudioInput
=
try
AVCaptureDeviceInput
(
device
:
AVCaptureDevice
.
defaultDevice
(
withMediaType
:
AVMediaTypeAudio
))
if
session
.
canAddInput
(
activeAudioInput
)
{
session
.
addInput
(
activeAudioInput
)
}
}
catch
let
e
as
NSError
{
delegate
?
.
captureSessionFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
captureSessionFailedWithError
?(
capture
Session
:
self
,
error
:
e
)
}
}
/**
:name: prepareImageOutput
*/
/// Prepares the imageOutput.
private
func
prepareImageOutput
()
{
if
session
.
canAddOutput
(
imageOutput
)
{
imageOutput
.
outputSettings
=
[
AVVideoCodecKey
:
AVVideoCodecJPEG
]
imageOutput
=
AVCaptureStillImageOutput
()
imageOutput
.
outputSettings
=
[
AVVideoCodecKey
:
AVVideoCodecJPEG
]
session
.
addOutput
(
imageOutput
)
}
}
/**
:name: prepareMovieOutput
*/
/// Prepares the movieOutput.
private
func
prepareMovieOutput
()
{
if
session
.
canAddOutput
(
movieOutput
)
{
movieOutput
=
AVCaptureMovieFileOutput
()
session
.
addOutput
(
movieOutput
)
}
}
/**
:name: cameraWithPosition
*/
private
func
cameraWithPosition
(
position
:
AVCaptureDevicePosition
)
->
AVCaptureDevice
?
{
A reference to the camera at a given position, if one exists.
- Parameter at: An AVCaptureDevicePosition.
- Returns: An AVCaptureDevice if one exists, or nil otherwise.
*/
private
func
camera
(
at
position
:
AVCaptureDevicePosition
)
->
AVCaptureDevice
?
{
let
devices
:
Array
<
AVCaptureDevice
>
=
AVCaptureDevice
.
devices
(
withMediaType
:
AVMediaTypeVideo
)
as!
Array
<
AVCaptureDevice
>
for
device
in
devices
{
if
device
.
position
==
position
{
...
...
@@ -752,7 +716,8 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
}
/**
:name: uniqueURL
Creates a unique URL if possible.
- Returns: A NSURL if it is possible to create one.
*/
private
func
uniqueURL
()
->
NSURL
?
{
do
{
...
...
@@ -762,7 +727,7 @@ public class CaptureSession: NSObject, AVCaptureFileOutputRecordingDelegate {
dateFormatter
.
timeStyle
=
.
fullStyle
return
directory
.
appendingPathComponent
(
dateFormatter
.
string
(
from
:
NSDate
()
as
Date
)
+
".mov"
)
}
catch
let
e
as
NSError
{
delegate
?
.
capture
CreateMovieFileFailedWithError
?(
capture
:
self
,
error
:
e
)
delegate
?
.
capture
SessionCreateMovieFileFailedWithError
?(
captureSession
:
self
,
error
:
e
)
}
return
nil
}
...
...
Sources/iOS/CaptureView.swift
View file @
22717582
...
...
@@ -433,9 +433,9 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
*/
@objc
internal
func
handleTapToFocusGesture
(
recognizer
:
UITapGestureRecognizer
)
{
if
enableTapToFocus
&&
captureSession
.
cameraSupportsTapToFocus
{
if
enableTapToFocus
&&
captureSession
.
isFocusPointOfInterestSupported
{
let
point
:
CGPoint
=
recognizer
.
location
(
in
:
self
)
captureSession
.
focus
AtPoint
(
point
:
previewView
.
captureDevicePointOfInterestForPoint
(
point
:
point
))
captureSession
.
focus
(
point
:
previewView
.
captureDevicePointOfInterestForPoint
(
point
:
point
))
animateTapLayer
(
layer
:
focusLayer
!
,
point
:
point
)
(
delegate
as?
CaptureViewDelegate
)?
.
captureViewDidTapToFocusAtPoint
?(
captureView
:
self
,
point
:
point
)
}
...
...
@@ -447,9 +447,9 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
*/
@objc
internal
func
handleTapToExposeGesture
(
recognizer
:
UITapGestureRecognizer
)
{
if
enableTapToExpose
&&
captureSession
.
cameraSupportsTapToExpose
{
if
enableTapToExpose
&&
captureSession
.
isExposurePointOfInterestSupported
{
let
point
:
CGPoint
=
recognizer
.
location
(
in
:
self
)
captureSession
.
expose
AtPoint
(
point
:
previewView
.
captureDevicePointOfInterestForPoint
(
point
:
point
))
captureSession
.
expose
(
point
:
previewView
.
captureDevicePointOfInterestForPoint
(
point
:
point
))
animateTapLayer
(
layer
:
exposureLayer
!
,
point
:
point
)
(
delegate
as?
CaptureViewDelegate
)?
.
captureViewDidTapToExposeAtPoint
?(
captureView
:
self
,
point
:
point
)
}
...
...
@@ -462,7 +462,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
@objc
internal
func
handleTapToResetGesture
(
recognizer
:
UITapGestureRecognizer
)
{
if
enableTapToReset
{
captureSession
.
reset
FocusAndExposureModes
()
captureSession
.
reset
()
let
point
:
CGPoint
=
previewView
.
pointForCaptureDevicePointOfInterest
(
point
:
CGPoint
(
x
:
0.5
,
y
:
0.5
))
animateTapLayer
(
layer
:
resetLayer
!
,
point
:
point
)
(
delegate
as?
CaptureViewDelegate
)?
.
captureViewDidTapToResetAtPoint
?(
captureView
:
self
,
point
:
point
)
...
...
Sources/iOS/ControlView.swift
View file @
22717582
...
...
@@ -59,7 +59,7 @@ public class ControlView : MaterialView {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
Sources/iOS/Grid.swift
View file @
22717582
...
...
@@ -156,7 +156,7 @@ public class Grid {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
@@ -252,4 +252,4 @@ public extension UIView {
associateObject
(
self
,
key
:
&
GridKey
,
value
:
value
)
}
}
}
\ No newline at end of file
}
Sources/iOS/InterimSpace.swift
View file @
22717582
...
...
@@ -44,7 +44,7 @@ public enum InterimSpace {
}
/// Converts the InterimSpace enum to a CGFloat value.
public
func
i
nterimSpaceToValue
(
interimSpace
:
InterimSpace
)
->
CGFloat
{
public
func
I
nterimSpaceToValue
(
interimSpace
:
InterimSpace
)
->
CGFloat
{
switch
interimSpace
{
case
.
none
:
return
0
...
...
Sources/iOS/MaterialCollectionReusableView.swift
View file @
22717582
...
...
@@ -155,7 +155,7 @@ public class MaterialCollectionReusableView : UICollectionReusableView {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
@@ -606,4 +606,4 @@ public class MaterialCollectionReusableView : UICollectionReusableView {
}
}
}
}
\ No newline at end of file
}
Sources/iOS/MaterialCollectionView.swift
View file @
22717582
...
...
@@ -104,7 +104,7 @@ public class MaterialCollectionView : UICollectionView {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
@@ -163,4 +163,4 @@ public class MaterialCollectionView : UICollectionView {
backgroundColor
=
Color
.
clear
contentInset
=
UIEdgeInsetsZero
}
}
\ No newline at end of file
}
Sources/iOS/MaterialCollectionViewCell.swift
View file @
22717582
...
...
@@ -155,7 +155,7 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
@@ -605,4 +605,4 @@ public class MaterialCollectionViewCell : UICollectionViewCell {
}
}
}
}
\ No newline at end of file
}
Sources/iOS/MaterialCollectionViewLayout.swift
View file @
22717582
...
...
@@ -62,7 +62,7 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
@@ -159,4 +159,4 @@ public class MaterialCollectionViewLayout : UICollectionViewLayout {
contentSize
=
CGSizeMake
(
offset
.
x
,
collectionView
!.
bounds
.
height
)
}
}
}
\ No newline at end of file
}
Sources/iOS/Menu.swift
View file @
22717582
...
...
@@ -51,7 +51,7 @@ public class Menu {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
Sources/iOS/NavigationBar.swift
View file @
22717582
...
...
@@ -78,7 +78,7 @@ public class NavigationBar : UINavigationBar {
/// A preset wrapper around interimSpace.
public
var
interimSpacePreset
:
InterimSpace
=
.
none
{
didSet
{
interimSpace
=
i
nterimSpaceToValue
(
interimSpacePreset
)
interimSpace
=
I
nterimSpaceToValue
(
interimSpacePreset
)
}
}
...
...
@@ -475,4 +475,4 @@ public class NavigationBar : UINavigationBar {
item
.
contentView
!.
grid
.
axis
.
direction
=
.
Vertical
return
item
.
contentView
!
}
}
\ No newline at end of file
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment