├── FrameExtraction.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── Bobo.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── FrameExtraction.xcscheme │ └── xcschememanagement.plist ├── FrameExtraction ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── FrameExtractor.swift ├── Info.plist └── ViewController.swift ├── Inside Serial Queue (ci - cg - ui - global context).png ├── Inside Serial Queue (ci - cg - ui).png ├── Inside serial queue - UIImage(ciImage).png └── Outside serial queue (ci - cg - ui).png /FrameExtraction.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C3E867761E1488470042F78A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3E867751E1488470042F78A /* AppDelegate.swift */; }; 11 | C3E867781E1488470042F78A /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3E867771E1488470042F78A /* ViewController.swift */; }; 12 | C3E8677B1E1488470042F78A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C3E867791E1488470042F78A /* Main.storyboard */; }; 13 | C3E8677D1E1488470042F78A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C3E8677C1E1488470042F78A /* Assets.xcassets */; }; 14 | C3E867801E1488470042F78A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C3E8677E1E1488470042F78A /* LaunchScreen.storyboard */; }; 15 | C3E867881E14898D0042F78A /* FrameExtractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3E867871E14898D0042F78A /* FrameExtractor.swift */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | C3E867721E1488470042F78A /* FrameExtraction.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FrameExtraction.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | C3E867751E1488470042F78A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 21 | C3E867771E1488470042F78A /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 22 | C3E8677A1E1488470042F78A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 23 | C3E8677C1E1488470042F78A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | C3E8677F1E1488470042F78A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 25 | C3E867811E1488470042F78A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 26 | C3E867871E14898D0042F78A /* FrameExtractor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FrameExtractor.swift; sourceTree = ""; }; 27 | /* End PBXFileReference section */ 28 | 29 | /* Begin PBXFrameworksBuildPhase section */ 30 | C3E8676F1E1488470042F78A /* Frameworks */ = { 31 | isa = PBXFrameworksBuildPhase; 32 | buildActionMask = 2147483647; 33 | files = ( 34 | ); 35 | runOnlyForDeploymentPostprocessing = 0; 36 | }; 37 | /* End PBXFrameworksBuildPhase section */ 38 | 39 | /* Begin PBXGroup section */ 40 | C3E867691E1488470042F78A = { 41 | isa = PBXGroup; 42 | children = ( 43 | C3E867741E1488470042F78A /* FrameExtraction */, 44 | C3E867731E1488470042F78A /* Products */, 45 | ); 46 | sourceTree = ""; 47 | }; 48 | C3E867731E1488470042F78A /* Products */ = { 49 | isa = PBXGroup; 50 | children = ( 51 | C3E867721E1488470042F78A /* FrameExtraction.app */, 52 | ); 53 | name = Products; 54 | sourceTree = ""; 55 | }; 56 | C3E867741E1488470042F78A /* FrameExtraction */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | C3E867751E1488470042F78A /* AppDelegate.swift */, 60 | C3E867771E1488470042F78A /* ViewController.swift */, 61 | C3E867871E14898D0042F78A /* FrameExtractor.swift */, 62 | C3E867791E1488470042F78A /* Main.storyboard */, 63 | C3E8677C1E1488470042F78A /* Assets.xcassets */, 64 | C3E8677E1E1488470042F78A /* LaunchScreen.storyboard */, 65 | C3E867811E1488470042F78A /* Info.plist */, 66 | ); 67 | path = FrameExtraction; 68 | sourceTree = ""; 69 | }; 70 | /* End PBXGroup section */ 71 | 72 | /* Begin PBXNativeTarget section */ 73 | C3E867711E1488470042F78A /* FrameExtraction */ = { 74 | isa = PBXNativeTarget; 75 | buildConfigurationList = C3E867841E1488470042F78A /* Build configuration list for PBXNativeTarget "FrameExtraction" */; 76 | buildPhases = ( 77 | C3E8676E1E1488470042F78A /* Sources */, 78 | C3E8676F1E1488470042F78A /* Frameworks */, 79 | C3E867701E1488470042F78A /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = FrameExtraction; 86 | productName = FrameExtraction; 87 | productReference = C3E867721E1488470042F78A /* FrameExtraction.app */; 88 | productType = "com.apple.product-type.application"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | C3E8676A1E1488470042F78A /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0800; 97 | LastUpgradeCheck = 0800; 98 | ORGANIZATIONNAME = bRo; 99 | TargetAttributes = { 100 | C3E867711E1488470042F78A = { 101 | CreatedOnToolsVersion = 8.0; 102 | DevelopmentTeam = WEQK56686X; 103 | ProvisioningStyle = Automatic; 104 | }; 105 | }; 106 | }; 107 | buildConfigurationList = C3E8676D1E1488470042F78A /* Build configuration list for PBXProject "FrameExtraction" */; 108 | compatibilityVersion = "Xcode 3.2"; 109 | developmentRegion = English; 110 | hasScannedForEncodings = 0; 111 | knownRegions = ( 112 | en, 113 | Base, 114 | ); 115 | mainGroup = C3E867691E1488470042F78A; 116 | productRefGroup = C3E867731E1488470042F78A /* Products */; 117 | projectDirPath = ""; 118 | projectRoot = ""; 119 | targets = ( 120 | C3E867711E1488470042F78A /* FrameExtraction */, 121 | ); 122 | }; 123 | /* End PBXProject section */ 124 | 125 | /* Begin PBXResourcesBuildPhase section */ 126 | C3E867701E1488470042F78A /* Resources */ = { 127 | isa = PBXResourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | C3E867801E1488470042F78A /* LaunchScreen.storyboard in Resources */, 131 | C3E8677D1E1488470042F78A /* Assets.xcassets in Resources */, 132 | C3E8677B1E1488470042F78A /* Main.storyboard in Resources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXResourcesBuildPhase section */ 137 | 138 | /* Begin PBXSourcesBuildPhase section */ 139 | C3E8676E1E1488470042F78A /* Sources */ = { 140 | isa = PBXSourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | C3E867881E14898D0042F78A /* FrameExtractor.swift in Sources */, 144 | C3E867781E1488470042F78A /* ViewController.swift in Sources */, 145 | C3E867761E1488470042F78A /* AppDelegate.swift in Sources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXSourcesBuildPhase section */ 150 | 151 | /* Begin PBXVariantGroup section */ 152 | C3E867791E1488470042F78A /* Main.storyboard */ = { 153 | isa = PBXVariantGroup; 154 | children = ( 155 | C3E8677A1E1488470042F78A /* Base */, 156 | ); 157 | name = Main.storyboard; 158 | sourceTree = ""; 159 | }; 160 | C3E8677E1E1488470042F78A /* LaunchScreen.storyboard */ = { 161 | isa = PBXVariantGroup; 162 | children = ( 163 | C3E8677F1E1488470042F78A /* Base */, 164 | ); 165 | name = LaunchScreen.storyboard; 166 | sourceTree = ""; 167 | }; 168 | /* End PBXVariantGroup section */ 169 | 170 | /* Begin XCBuildConfiguration section */ 171 | C3E867821E1488470042F78A /* Debug */ = { 172 | isa = XCBuildConfiguration; 173 | buildSettings = { 174 | ALWAYS_SEARCH_USER_PATHS = NO; 175 | CLANG_ANALYZER_NONNULL = YES; 176 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 177 | CLANG_CXX_LIBRARY = "libc++"; 178 | CLANG_ENABLE_MODULES = YES; 179 | CLANG_ENABLE_OBJC_ARC = YES; 180 | CLANG_WARN_BOOL_CONVERSION = YES; 181 | CLANG_WARN_CONSTANT_CONVERSION = YES; 182 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 183 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 184 | CLANG_WARN_EMPTY_BODY = YES; 185 | CLANG_WARN_ENUM_CONVERSION = YES; 186 | CLANG_WARN_INFINITE_RECURSION = YES; 187 | CLANG_WARN_INT_CONVERSION = YES; 188 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 189 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 190 | CLANG_WARN_UNREACHABLE_CODE = YES; 191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 192 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 193 | COPY_PHASE_STRIP = NO; 194 | DEBUG_INFORMATION_FORMAT = dwarf; 195 | ENABLE_STRICT_OBJC_MSGSEND = YES; 196 | ENABLE_TESTABILITY = YES; 197 | GCC_C_LANGUAGE_STANDARD = gnu99; 198 | GCC_DYNAMIC_NO_PIC = NO; 199 | GCC_NO_COMMON_BLOCKS = YES; 200 | GCC_OPTIMIZATION_LEVEL = 0; 201 | GCC_PREPROCESSOR_DEFINITIONS = ( 202 | "DEBUG=1", 203 | "$(inherited)", 204 | ); 205 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 206 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 207 | GCC_WARN_UNDECLARED_SELECTOR = YES; 208 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 209 | GCC_WARN_UNUSED_FUNCTION = YES; 210 | GCC_WARN_UNUSED_VARIABLE = YES; 211 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 212 | MTL_ENABLE_DEBUG_INFO = YES; 213 | ONLY_ACTIVE_ARCH = YES; 214 | SDKROOT = iphoneos; 215 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 216 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 217 | }; 218 | name = Debug; 219 | }; 220 | C3E867831E1488470042F78A /* Release */ = { 221 | isa = XCBuildConfiguration; 222 | buildSettings = { 223 | ALWAYS_SEARCH_USER_PATHS = NO; 224 | CLANG_ANALYZER_NONNULL = YES; 225 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 226 | CLANG_CXX_LIBRARY = "libc++"; 227 | CLANG_ENABLE_MODULES = YES; 228 | CLANG_ENABLE_OBJC_ARC = YES; 229 | CLANG_WARN_BOOL_CONVERSION = YES; 230 | CLANG_WARN_CONSTANT_CONVERSION = YES; 231 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 232 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 233 | CLANG_WARN_EMPTY_BODY = YES; 234 | CLANG_WARN_ENUM_CONVERSION = YES; 235 | CLANG_WARN_INFINITE_RECURSION = YES; 236 | CLANG_WARN_INT_CONVERSION = YES; 237 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 238 | CLANG_WARN_SUSPICIOUS_MOVES = YES; 239 | CLANG_WARN_UNREACHABLE_CODE = YES; 240 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 241 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 242 | COPY_PHASE_STRIP = NO; 243 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 244 | ENABLE_NS_ASSERTIONS = NO; 245 | ENABLE_STRICT_OBJC_MSGSEND = YES; 246 | GCC_C_LANGUAGE_STANDARD = gnu99; 247 | GCC_NO_COMMON_BLOCKS = YES; 248 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 249 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 250 | GCC_WARN_UNDECLARED_SELECTOR = YES; 251 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 252 | GCC_WARN_UNUSED_FUNCTION = YES; 253 | GCC_WARN_UNUSED_VARIABLE = YES; 254 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 255 | MTL_ENABLE_DEBUG_INFO = NO; 256 | SDKROOT = iphoneos; 257 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 258 | VALIDATE_PRODUCT = YES; 259 | }; 260 | name = Release; 261 | }; 262 | C3E867851E1488470042F78A /* Debug */ = { 263 | isa = XCBuildConfiguration; 264 | buildSettings = { 265 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 266 | DEVELOPMENT_TEAM = WEQK56686X; 267 | INFOPLIST_FILE = FrameExtraction/Info.plist; 268 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 269 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 270 | PRODUCT_BUNDLE_IDENTIFIER = com.bro.FrameExtraction; 271 | PRODUCT_NAME = "$(TARGET_NAME)"; 272 | SWIFT_VERSION = 3.0; 273 | }; 274 | name = Debug; 275 | }; 276 | C3E867861E1488470042F78A /* Release */ = { 277 | isa = XCBuildConfiguration; 278 | buildSettings = { 279 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 280 | DEVELOPMENT_TEAM = WEQK56686X; 281 | INFOPLIST_FILE = FrameExtraction/Info.plist; 282 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 283 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 284 | PRODUCT_BUNDLE_IDENTIFIER = com.bro.FrameExtraction; 285 | PRODUCT_NAME = "$(TARGET_NAME)"; 286 | SWIFT_VERSION = 3.0; 287 | }; 288 | name = Release; 289 | }; 290 | /* End XCBuildConfiguration section */ 291 | 292 | /* Begin XCConfigurationList section */ 293 | C3E8676D1E1488470042F78A /* Build configuration list for PBXProject "FrameExtraction" */ = { 294 | isa = XCConfigurationList; 295 | buildConfigurations = ( 296 | C3E867821E1488470042F78A /* Debug */, 297 | C3E867831E1488470042F78A /* Release */, 298 | ); 299 | defaultConfigurationIsVisible = 0; 300 | defaultConfigurationName = Release; 301 | }; 302 | C3E867841E1488470042F78A /* Build configuration list for PBXNativeTarget "FrameExtraction" */ = { 303 | isa = XCConfigurationList; 304 | buildConfigurations = ( 305 | C3E867851E1488470042F78A /* Debug */, 306 | C3E867861E1488470042F78A /* Release */, 307 | ); 308 | defaultConfigurationIsVisible = 0; 309 | defaultConfigurationName = Release; 310 | }; 311 | /* End XCConfigurationList section */ 312 | }; 313 | rootObject = C3E8676A1E1488470042F78A /* Project object */; 314 | } 315 | -------------------------------------------------------------------------------- /FrameExtraction.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /FrameExtraction.xcodeproj/xcuserdata/Bobo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /FrameExtraction.xcodeproj/xcuserdata/Bobo.xcuserdatad/xcschemes/FrameExtraction.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /FrameExtraction.xcodeproj/xcuserdata/Bobo.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | FrameExtraction.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | C3E867711E1488470042F78A 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /FrameExtraction/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // FrameExtraction 4 | // 5 | // Created by Bobo on 29/12/2016. 6 | // Copyright © 2016 bRo. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 17 | // Override point for customization after application launch. 18 | return true 19 | } 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /FrameExtraction/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | } 43 | ], 44 | "info" : { 45 | "version" : 1, 46 | "author" : "xcode" 47 | } 48 | } -------------------------------------------------------------------------------- /FrameExtraction/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /FrameExtraction/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /FrameExtraction/FrameExtractor.swift: -------------------------------------------------------------------------------- 1 | // 2 | // FrameExtractor.swift 3 | // Created by Bobo on 29/12/2016. 4 | // 5 | 6 | import UIKit 7 | import AVFoundation 8 | 9 | protocol FrameExtractorDelegate: class { 10 | func captured(image: UIImage) 11 | } 12 | 13 | class FrameExtractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate { 14 | 15 | private var position = AVCaptureDevicePosition.back 16 | private let quality = AVCaptureSessionPresetMedium 17 | 18 | private var permissionGranted = false 19 | private let sessionQueue = DispatchQueue(label: "session queue") 20 | private let captureSession = AVCaptureSession() 21 | private let context = CIContext() 22 | 23 | weak var delegate: FrameExtractorDelegate? 24 | 25 | override init() { 26 | super.init() 27 | checkPermission() 28 | sessionQueue.async { [unowned self] in 29 | self.configureSession() 30 | self.captureSession.startRunning() 31 | } 32 | } 33 | 34 | public func flipCamera() { 35 | sessionQueue.async { [unowned self] in 36 | self.captureSession.beginConfiguration() 37 | guard let currentCaptureInput = self.captureSession.inputs.first as? AVCaptureInput else { return } 38 | self.captureSession.removeInput(currentCaptureInput) 39 | guard let currentCaptureOutput = self.captureSession.outputs.first as? AVCaptureOutput else { return } 40 | self.captureSession.removeOutput(currentCaptureOutput) 41 | self.position = self.position == .front ? .back : .front 42 | self.configureSession() 43 | self.captureSession.commitConfiguration() 44 | } 45 | } 46 | 47 | // MARK: AVSession configuration 48 | private func checkPermission() { 49 | switch AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) { 50 | case .authorized: 51 | permissionGranted = true 52 | case .notDetermined: 53 | requestPermission() 54 | default: 55 | permissionGranted = false 56 | } 57 | } 58 | 59 | private func requestPermission() { 60 | sessionQueue.suspend() 61 | AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { [unowned self] granted in 62 | self.permissionGranted = granted 63 | self.sessionQueue.resume() 64 | } 65 | } 66 | 67 | private func configureSession() { 68 | guard permissionGranted else { return } 69 | captureSession.sessionPreset = quality 70 | guard let captureDevice = selectCaptureDevice() else { return } 71 | guard let captureDeviceInput = try? AVCaptureDeviceInput(device: captureDevice) else { return } 72 | guard captureSession.canAddInput(captureDeviceInput) else { return } 73 | captureSession.addInput(captureDeviceInput) 74 | let videoOutput = AVCaptureVideoDataOutput() 75 | videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer")) 76 | guard captureSession.canAddOutput(videoOutput) else { return } 77 | captureSession.addOutput(videoOutput) 78 | guard let connection = videoOutput.connection(withMediaType: AVFoundation.AVMediaTypeVideo) else { return } 79 | guard connection.isVideoOrientationSupported else { return } 80 | guard connection.isVideoMirroringSupported else { return } 81 | connection.videoOrientation = .portrait 82 | connection.isVideoMirrored = position == .front 83 | } 84 | 85 | private func selectCaptureDevice() -> AVCaptureDevice? { 86 | return AVCaptureDevice.devices().filter { 87 | ($0 as AnyObject).hasMediaType(AVMediaTypeVideo) && 88 | ($0 as AnyObject).position == position 89 | }.first as? AVCaptureDevice 90 | } 91 | 92 | // MARK: Sample buffer to UIImage conversion 93 | private func imageFromSampleBuffer(sampleBuffer: CMSampleBuffer) -> UIImage? { 94 | guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return nil } 95 | let ciImage = CIImage(cvPixelBuffer: imageBuffer) 96 | guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { return nil } 97 | return UIImage(cgImage: cgImage) 98 | } 99 | 100 | // MARK: AVCaptureVideoDataOutputSampleBufferDelegate 101 | func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) { 102 | guard let uiImage = imageFromSampleBuffer(sampleBuffer: sampleBuffer) else { return } 103 | DispatchQueue.main.async { [unowned self] in 104 | self.delegate?.captured(image: uiImage) 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /FrameExtraction/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | NSCameraUsageDescription 24 | Used to capture frames 25 | LSApplicationCategoryType 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | Main 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /FrameExtraction/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Created by Bobo on 29/12/2016. 4 | // 5 | 6 | import UIKit 7 | 8 | class ViewController: UIViewController, FrameExtractorDelegate { 9 | 10 | var frameExtractor: FrameExtractor! 11 | 12 | @IBOutlet weak var imageView: UIImageView! 13 | 14 | @IBAction func flipButton(_ sender: UIButton) { 15 | frameExtractor.flipCamera() 16 | } 17 | 18 | override func viewDidLoad() { 19 | super.viewDidLoad() 20 | frameExtractor = FrameExtractor() 21 | frameExtractor.delegate = self 22 | } 23 | 24 | func captured(image: UIImage) { 25 | imageView.image = image 26 | } 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Inside Serial Queue (ci - cg - ui - global context).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/b-r-o/FrameExtractor/9e0984fb0b48cce60d073a896efc2b3d7867d996/Inside Serial Queue (ci - cg - ui - global context).png -------------------------------------------------------------------------------- /Inside Serial Queue (ci - cg - ui).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/b-r-o/FrameExtractor/9e0984fb0b48cce60d073a896efc2b3d7867d996/Inside Serial Queue (ci - cg - ui).png -------------------------------------------------------------------------------- /Inside serial queue - UIImage(ciImage).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/b-r-o/FrameExtractor/9e0984fb0b48cce60d073a896efc2b3d7867d996/Inside serial queue - UIImage(ciImage).png -------------------------------------------------------------------------------- /Outside serial queue (ci - cg - ui).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/b-r-o/FrameExtractor/9e0984fb0b48cce60d073a896efc2b3d7867d996/Outside serial queue (ci - cg - ui).png --------------------------------------------------------------------------------