-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from carousell/feature/live-preview
Live Preview
- Loading branch information
Showing
19 changed files
with
282 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Pickle.xcodeproj/xcshareddata/xcschemes/Pickle-Example.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
Pickle/Assets/Images.xcassets/camera-icon.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"filename" : "[email protected]", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"version" : 1, | ||
"author" : "xcode" | ||
} | ||
} |
Binary file added
BIN
+910 Bytes
Pickle/Assets/Images.xcassets/camera-icon.imageset/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// This source file is part of the carousell/pickle open source project | ||
// | ||
// Copyright © 2019 Carousell and the project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See https://github.com/carousell/pickle/blob/master/LICENSE for license information | ||
// See https://github.com/carousell/pickle/graphs/contributors for the list of project authors | ||
// | ||
|
||
|
||
import Foundation | ||
import AVFoundation | ||
|
||
final class CameraSessionHandler { | ||
|
||
enum CameraSessionError: Error { | ||
case noDeviceInput, noPermission | ||
} | ||
|
||
private let session = AVCaptureSession() | ||
|
||
weak var previewView: LiveView? { | ||
didSet { | ||
previewView?.session = session | ||
} | ||
} | ||
|
||
var isRunning: Bool { | ||
return session.isRunning | ||
} | ||
|
||
var hasPermssion: Bool { | ||
return AVCaptureDevice.authorizationStatus(for: .video) == .authorized | ||
} | ||
|
||
private let sessionQueue = DispatchQueue(label: "pickle.liveView.sessionQueue") | ||
|
||
init() throws { | ||
guard hasPermssion else { throw CameraSessionError.noPermission } | ||
} | ||
|
||
func startSession() throws { | ||
guard | ||
let input = AVCaptureDevice.default(for: .video), | ||
let deviceInput = try? AVCaptureDeviceInput(device: input) else { | ||
throw CameraSessionError.noDeviceInput | ||
} | ||
if session.inputs.count == 0 { | ||
session.addInput(deviceInput) | ||
} | ||
sessionQueue.async { [weak self] in | ||
guard self?.session.isRunning == .some(false) else { return } | ||
self?.session.startRunning() | ||
} | ||
} | ||
|
||
func stopSession() { | ||
sessionQueue.async { [weak self] in | ||
self?.session.stopRunning() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// This source file is part of the carousell/pickle open source project | ||
// | ||
// Copyright © 2019 Carousell and the project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See https://github.com/carousell/pickle/blob/master/LICENSE for license information | ||
// See https://github.com/carousell/pickle/graphs/contributors for the list of project authors | ||
// | ||
|
||
import AVFoundation | ||
import UIKit | ||
|
||
final class LiveView: UIView { | ||
|
||
override class var layerClass: AnyClass { | ||
return AVCaptureVideoPreviewLayer.self | ||
} | ||
|
||
private var videoPreviewLayer: AVCaptureVideoPreviewLayer { | ||
guard let layer = self.layer as? AVCaptureVideoPreviewLayer else { | ||
fatalError("Expected `AVCaptureVideoPreviewLayer` type for layer. Check LiveView.layerClass implementation.") | ||
} | ||
layer.videoGravity = .resizeAspectFill | ||
return layer | ||
} | ||
|
||
var session: AVCaptureSession? { | ||
get { | ||
return videoPreviewLayer.session | ||
} | ||
set { | ||
videoPreviewLayer.session = newValue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.