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
b957aa1e
Commit
b957aa1e
authored
Nov 23, 2015
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Capture is born. Working CaptureSession and CapturePreviewView
parent
47e5a079
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
136 additions
and
18 deletions
+136
-18
Source/CapturePreviewView.swift
+29
-10
Source/CaptureSession.swift
+107
-8
No files found.
Source/CapturePreviewView.swift
View file @
b957aa1e
...
...
@@ -21,11 +21,9 @@ import AVFoundation
public
class
CapturePreviewView
:
MaterialView
{
/**
:name:
layerClass
:name:
previewLayer
*/
public
override
class
func
layerClass
()
->
AnyClass
{
return
AVCaptureVideoPreviewLayer
.
self
}
public
private(set)
lazy
var
previewLayer
:
AVCaptureVideoPreviewLayer
=
AVCaptureVideoPreviewLayer
()
/**
:name: capture
...
...
@@ -33,31 +31,51 @@ public class CapturePreviewView : MaterialView {
public
private(set)
lazy
var
captureSession
:
CaptureSession
=
CaptureSession
()
/**
:name: layoutSublayersOfLayer
*/
public
override
func
layoutSublayersOfLayer
(
layer
:
CALayer
)
{
super
.
layoutSublayersOfLayer
(
layer
)
if
self
.
layer
==
layer
{
layoutPreviewLayer
()
}
}
/**
:name: prepareView
*/
public
override
func
prepareView
()
{
super
.
prepareView
()
prepare
Session
()
prepare
PreviewLayer
()
}
/**
:name: captureDevicePointOfInterestForPoint
*/
public
func
captureDevicePointOfInterestForPoint
(
point
:
CGPoint
)
->
CGPoint
{
return
(
layer
as!
AVCaptureVideoPreviewLayer
)
.
captureDevicePointOfInterestForPoint
(
point
)
return
previewLayer
.
captureDevicePointOfInterestForPoint
(
point
)
}
/**
:name: pointForCaptureDevicePointOfInterest
*/
public
func
pointForCaptureDevicePointOfInterest
(
point
:
CGPoint
)
->
CGPoint
{
return
(
layer
as!
AVCaptureVideoPreviewLayer
)
.
pointForCaptureDevicePointOfInterest
(
point
)
return
previewLayer
.
pointForCaptureDevicePointOfInterest
(
point
)
}
//
// :name: preparePreviewLayer
//
private
func
preparePreviewLayer
()
{
previewLayer
.
session
=
captureSession
.
session
visualLayer
.
addSublayer
(
previewLayer
)
}
//
// :name:
prepareSession
// :name:
layoutPreviewLayer
//
private
func
prepareSession
()
{
(
layer
as!
AVCaptureVideoPreviewLayer
)
.
session
=
captureSession
.
session
internal
func
layoutPreviewLayer
()
{
previewLayer
.
frame
=
visualLayer
.
bounds
previewLayer
.
position
=
CGPointMake
(
width
/
2
,
height
/
2
)
previewLayer
.
cornerRadius
=
visualLayer
.
cornerRadius
}
}
\ No newline at end of file
Source/CaptureSession.swift
View file @
b957aa1e
...
...
@@ -19,6 +19,20 @@
import
UIKit
import
AVFoundation
public
enum
CaptureSessionPreset
{
case
High
}
/**
:name: CaptureSessionPresetToString
*/
public
func
CaptureSessionPresetToString
(
preset
:
CaptureSessionPreset
)
->
String
{
switch
preset
{
case
.
High
:
return
AVCaptureSessionPresetHigh
}
}
@objc(CaptureSessionDelegate)
public
protocol
CaptureSessionDelegate
{
/**
...
...
@@ -64,10 +78,10 @@ public class CaptureSession : NSObject {
//
private
lazy
var
movieOutput
:
AVCaptureMovieFileOutput
=
AVCaptureMovieFileOutput
()
/
**
:name: session
*
/
public
private(set)
lazy
var
session
:
AVCaptureSession
=
AVCaptureSession
()
/
/
//
:name: session
/
/
internal
lazy
var
session
:
AVCaptureSession
=
AVCaptureSession
()
/**
:name: isRunning
...
...
@@ -75,6 +89,50 @@ public class CaptureSession : NSObject {
public
private(set)
lazy
var
isRunning
:
Bool
=
false
/**
:name: activeCamera
*/
public
var
activeCamera
:
AVCaptureDevice
?
{
return
videoInput
?
.
device
}
/**
:name: init
*/
public
override
init
()
{
super
.
init
()
prepareSession
()
}
/**
:name: inactiveCamera
*/
public
var
inactiveCamera
:
AVCaptureDevice
?
{
var
device
:
AVCaptureDevice
?
if
1
<
cameraCount
{
if
activeCamera
?
.
position
==
.
Back
{
device
=
cameraWithPosition
(
.
Front
)
}
else
if
activeCamera
?
.
position
==
.
Front
{
device
=
cameraWithPosition
(
.
Back
)
}
}
return
device
}
/**
:name: cameraCount
*/
public
var
cameraCount
:
Int
{
return
AVCaptureDevice
.
devicesWithMediaType
(
AVMediaTypeVideo
)
.
count
}
/**
:name: canSwitchCameras
*/
public
var
canSwitchCameras
:
Bool
{
return
1
<
cameraCount
}
/**
:name: delegate
*/
public
weak
var
delegate
:
CaptureSessionDelegate
?
...
...
@@ -82,7 +140,7 @@ public class CaptureSession : NSObject {
/**
:name: startSession
*/
public
func
startSess
t
ion
()
{
public
func
startSession
()
{
if
!
isRunning
{
dispatch_async
(
videoQueue
)
{
self
.
session
.
startRunning
()
...
...
@@ -93,7 +151,7 @@ public class CaptureSession : NSObject {
/**
:name: startSession
*/
public
func
stopSess
t
ion
()
{
public
func
stopSession
()
{
if
isRunning
{
dispatch_async
(
videoQueue
)
{
self
.
session
.
stopRunning
()
...
...
@@ -101,12 +159,40 @@ public class CaptureSession : NSObject {
}
}
/**
:name: sessionPreset
*/
public
func
sessionPreset
(
preset
:
CaptureSessionPreset
)
{
session
.
sessionPreset
=
CaptureSessionPresetToString
(
preset
)
}
/**
:name: switchCameras
*/
public
func
switchCameras
()
->
Bool
{
if
canSwitchCameras
{
do
{
let
vi
:
AVCaptureDeviceInput
?
=
try
AVCaptureDeviceInput
(
device
:
inactiveCamera
!
)
if
session
.
canAddInput
(
vi
)
{
session
.
beginConfiguration
()
session
.
removeInput
(
videoInput
)
session
.
addInput
(
vi
)
videoInput
=
vi
session
.
commitConfiguration
()
return
true
}
}
catch
let
e
as
NSError
{
delegate
?
.
captureSessionFailedWithError
?(
self
,
error
:
e
)
}
}
return
false
}
//
// :name: prepareSession
//
private
func
prepareSession
()
{
session
.
sessionPreset
=
AVCaptureSessionPresetHigh
sessionPreset
(
.
High
)
prepareVideoInput
()
prepareAudioInput
()
prepareImageOutput
()
...
...
@@ -159,4 +245,17 @@ public class CaptureSession : NSObject {
session
.
addOutput
(
movieOutput
)
}
}
//
// :name: cameraWithPosition
//
private
func
cameraWithPosition
(
position
:
AVCaptureDevicePosition
)
->
AVCaptureDevice
?
{
let
devices
:
Array
<
AVCaptureDevice
>
=
AVCaptureDevice
.
devicesWithMediaType
(
AVMediaTypeVideo
)
as!
Array
<
AVCaptureDevice
>
for
device
in
devices
{
if
device
.
position
==
position
{
return
device
}
}
return
nil
}
}
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