├── .gitignore ├── AVCaptureSessionSample.xcodeproj ├── project.pbxproj └── xcuserdata │ └── linhcn.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── AVCaptureSessionSample.xcscheme │ └── xcschememanagement.plist ├── AVCaptureSessionSample ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist └── ViewController.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | AVCaptureSessionSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata 2 | AVCaptureSessionSample.xcodeproj/xcuserdata/linh.chu.xcuserdatad/xcschemes/xcschememanagement.plist 3 | -------------------------------------------------------------------------------- /AVCaptureSessionSample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 87AF5AD71F09DE09000699C1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87AF5AD61F09DE09000699C1 /* AppDelegate.swift */; }; 11 | 87AF5AD91F09DE09000699C1 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 87AF5AD81F09DE09000699C1 /* ViewController.swift */; }; 12 | 87AF5ADC1F09DE09000699C1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 87AF5ADA1F09DE09000699C1 /* Main.storyboard */; }; 13 | 87AF5ADE1F09DE09000699C1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 87AF5ADD1F09DE09000699C1 /* Assets.xcassets */; }; 14 | 87AF5AE11F09DE09000699C1 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 87AF5ADF1F09DE09000699C1 /* LaunchScreen.storyboard */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | 87AF5AD31F09DE09000699C1 /* AVCaptureSessionSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AVCaptureSessionSample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | 87AF5AD61F09DE09000699C1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | 87AF5AD81F09DE09000699C1 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | 87AF5ADB1F09DE09000699C1 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | 87AF5ADD1F09DE09000699C1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | 87AF5AE01F09DE09000699C1 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | 87AF5AE21F09DE09000699C1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | /* End PBXFileReference section */ 26 | 27 | /* Begin PBXFrameworksBuildPhase section */ 28 | 87AF5AD01F09DE09000699C1 /* Frameworks */ = { 29 | isa = PBXFrameworksBuildPhase; 30 | buildActionMask = 2147483647; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 0; 34 | }; 35 | /* End PBXFrameworksBuildPhase section */ 36 | 37 | /* Begin PBXGroup section */ 38 | 87AF5ACA1F09DE09000699C1 = { 39 | isa = PBXGroup; 40 | children = ( 41 | 87AF5AD51F09DE09000699C1 /* AVCaptureSessionSample */, 42 | 87AF5AD41F09DE09000699C1 /* Products */, 43 | ); 44 | sourceTree = ""; 45 | }; 46 | 87AF5AD41F09DE09000699C1 /* Products */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | 87AF5AD31F09DE09000699C1 /* AVCaptureSessionSample.app */, 50 | ); 51 | name = Products; 52 | sourceTree = ""; 53 | }; 54 | 87AF5AD51F09DE09000699C1 /* AVCaptureSessionSample */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 87AF5AD61F09DE09000699C1 /* AppDelegate.swift */, 58 | 87AF5AD81F09DE09000699C1 /* ViewController.swift */, 59 | 87AF5ADA1F09DE09000699C1 /* Main.storyboard */, 60 | 87AF5ADD1F09DE09000699C1 /* Assets.xcassets */, 61 | 87AF5ADF1F09DE09000699C1 /* LaunchScreen.storyboard */, 62 | 87AF5AE21F09DE09000699C1 /* Info.plist */, 63 | ); 64 | path = AVCaptureSessionSample; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | 87AF5AD21F09DE09000699C1 /* AVCaptureSessionSample */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = 87AF5AE51F09DE09000699C1 /* Build configuration list for PBXNativeTarget "AVCaptureSessionSample" */; 73 | buildPhases = ( 74 | 87AF5ACF1F09DE09000699C1 /* Sources */, 75 | 87AF5AD01F09DE09000699C1 /* Frameworks */, 76 | 87AF5AD11F09DE09000699C1 /* Resources */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = AVCaptureSessionSample; 83 | productName = AVCaptureSessionSample; 84 | productReference = 87AF5AD31F09DE09000699C1 /* AVCaptureSessionSample.app */; 85 | productType = "com.apple.product-type.application"; 86 | }; 87 | /* End PBXNativeTarget section */ 88 | 89 | /* Begin PBXProject section */ 90 | 87AF5ACB1F09DE09000699C1 /* Project object */ = { 91 | isa = PBXProject; 92 | attributes = { 93 | LastSwiftUpdateCheck = 0830; 94 | LastUpgradeCheck = 0910; 95 | ORGANIZATIONNAME = linhcn; 96 | TargetAttributes = { 97 | 87AF5AD21F09DE09000699C1 = { 98 | CreatedOnToolsVersion = 8.3.2; 99 | DevelopmentTeam = QXETRBWFB2; 100 | LastSwiftMigration = 0910; 101 | ProvisioningStyle = Automatic; 102 | }; 103 | }; 104 | }; 105 | buildConfigurationList = 87AF5ACE1F09DE09000699C1 /* Build configuration list for PBXProject "AVCaptureSessionSample" */; 106 | compatibilityVersion = "Xcode 3.2"; 107 | developmentRegion = English; 108 | hasScannedForEncodings = 0; 109 | knownRegions = ( 110 | en, 111 | Base, 112 | ); 113 | mainGroup = 87AF5ACA1F09DE09000699C1; 114 | productRefGroup = 87AF5AD41F09DE09000699C1 /* Products */; 115 | projectDirPath = ""; 116 | projectRoot = ""; 117 | targets = ( 118 | 87AF5AD21F09DE09000699C1 /* AVCaptureSessionSample */, 119 | ); 120 | }; 121 | /* End PBXProject section */ 122 | 123 | /* Begin PBXResourcesBuildPhase section */ 124 | 87AF5AD11F09DE09000699C1 /* Resources */ = { 125 | isa = PBXResourcesBuildPhase; 126 | buildActionMask = 2147483647; 127 | files = ( 128 | 87AF5AE11F09DE09000699C1 /* LaunchScreen.storyboard in Resources */, 129 | 87AF5ADE1F09DE09000699C1 /* Assets.xcassets in Resources */, 130 | 87AF5ADC1F09DE09000699C1 /* Main.storyboard in Resources */, 131 | ); 132 | runOnlyForDeploymentPostprocessing = 0; 133 | }; 134 | /* End PBXResourcesBuildPhase section */ 135 | 136 | /* Begin PBXSourcesBuildPhase section */ 137 | 87AF5ACF1F09DE09000699C1 /* Sources */ = { 138 | isa = PBXSourcesBuildPhase; 139 | buildActionMask = 2147483647; 140 | files = ( 141 | 87AF5AD91F09DE09000699C1 /* ViewController.swift in Sources */, 142 | 87AF5AD71F09DE09000699C1 /* AppDelegate.swift in Sources */, 143 | ); 144 | runOnlyForDeploymentPostprocessing = 0; 145 | }; 146 | /* End PBXSourcesBuildPhase section */ 147 | 148 | /* Begin PBXVariantGroup section */ 149 | 87AF5ADA1F09DE09000699C1 /* Main.storyboard */ = { 150 | isa = PBXVariantGroup; 151 | children = ( 152 | 87AF5ADB1F09DE09000699C1 /* Base */, 153 | ); 154 | name = Main.storyboard; 155 | sourceTree = ""; 156 | }; 157 | 87AF5ADF1F09DE09000699C1 /* LaunchScreen.storyboard */ = { 158 | isa = PBXVariantGroup; 159 | children = ( 160 | 87AF5AE01F09DE09000699C1 /* Base */, 161 | ); 162 | name = LaunchScreen.storyboard; 163 | sourceTree = ""; 164 | }; 165 | /* End PBXVariantGroup section */ 166 | 167 | /* Begin XCBuildConfiguration section */ 168 | 87AF5AE31F09DE09000699C1 /* Debug */ = { 169 | isa = XCBuildConfiguration; 170 | buildSettings = { 171 | ALWAYS_SEARCH_USER_PATHS = NO; 172 | CLANG_ANALYZER_NONNULL = YES; 173 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 174 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 175 | CLANG_CXX_LIBRARY = "libc++"; 176 | CLANG_ENABLE_MODULES = YES; 177 | CLANG_ENABLE_OBJC_ARC = YES; 178 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 179 | CLANG_WARN_BOOL_CONVERSION = YES; 180 | CLANG_WARN_COMMA = 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_NON_LITERAL_NULL_CONVERSION = YES; 189 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 190 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 191 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 192 | CLANG_WARN_STRICT_PROTOTYPES = YES; 193 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 194 | CLANG_WARN_UNREACHABLE_CODE = YES; 195 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 196 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 197 | COPY_PHASE_STRIP = NO; 198 | DEBUG_INFORMATION_FORMAT = dwarf; 199 | ENABLE_STRICT_OBJC_MSGSEND = YES; 200 | ENABLE_TESTABILITY = YES; 201 | GCC_C_LANGUAGE_STANDARD = gnu99; 202 | GCC_DYNAMIC_NO_PIC = NO; 203 | GCC_NO_COMMON_BLOCKS = YES; 204 | GCC_OPTIMIZATION_LEVEL = 0; 205 | GCC_PREPROCESSOR_DEFINITIONS = ( 206 | "DEBUG=1", 207 | "$(inherited)", 208 | ); 209 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 210 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 211 | GCC_WARN_UNDECLARED_SELECTOR = YES; 212 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 213 | GCC_WARN_UNUSED_FUNCTION = YES; 214 | GCC_WARN_UNUSED_VARIABLE = YES; 215 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 216 | MTL_ENABLE_DEBUG_INFO = YES; 217 | ONLY_ACTIVE_ARCH = YES; 218 | SDKROOT = iphoneos; 219 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 220 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 221 | TARGETED_DEVICE_FAMILY = "1,2"; 222 | }; 223 | name = Debug; 224 | }; 225 | 87AF5AE41F09DE09000699C1 /* Release */ = { 226 | isa = XCBuildConfiguration; 227 | buildSettings = { 228 | ALWAYS_SEARCH_USER_PATHS = NO; 229 | CLANG_ANALYZER_NONNULL = YES; 230 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 231 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 232 | CLANG_CXX_LIBRARY = "libc++"; 233 | CLANG_ENABLE_MODULES = YES; 234 | CLANG_ENABLE_OBJC_ARC = YES; 235 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 236 | CLANG_WARN_BOOL_CONVERSION = YES; 237 | CLANG_WARN_COMMA = YES; 238 | CLANG_WARN_CONSTANT_CONVERSION = YES; 239 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 240 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 241 | CLANG_WARN_EMPTY_BODY = YES; 242 | CLANG_WARN_ENUM_CONVERSION = YES; 243 | CLANG_WARN_INFINITE_RECURSION = YES; 244 | CLANG_WARN_INT_CONVERSION = YES; 245 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 246 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 247 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 248 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 249 | CLANG_WARN_STRICT_PROTOTYPES = YES; 250 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 251 | CLANG_WARN_UNREACHABLE_CODE = YES; 252 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 253 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 254 | COPY_PHASE_STRIP = NO; 255 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 256 | ENABLE_NS_ASSERTIONS = NO; 257 | ENABLE_STRICT_OBJC_MSGSEND = YES; 258 | GCC_C_LANGUAGE_STANDARD = gnu99; 259 | GCC_NO_COMMON_BLOCKS = YES; 260 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 261 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 262 | GCC_WARN_UNDECLARED_SELECTOR = YES; 263 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 264 | GCC_WARN_UNUSED_FUNCTION = YES; 265 | GCC_WARN_UNUSED_VARIABLE = YES; 266 | IPHONEOS_DEPLOYMENT_TARGET = 10.3; 267 | MTL_ENABLE_DEBUG_INFO = NO; 268 | SDKROOT = iphoneos; 269 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 270 | TARGETED_DEVICE_FAMILY = "1,2"; 271 | VALIDATE_PRODUCT = YES; 272 | }; 273 | name = Release; 274 | }; 275 | 87AF5AE61F09DE09000699C1 /* Debug */ = { 276 | isa = XCBuildConfiguration; 277 | buildSettings = { 278 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 279 | DEVELOPMENT_TEAM = QXETRBWFB2; 280 | INFOPLIST_FILE = AVCaptureSessionSample/Info.plist; 281 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 282 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 283 | PRODUCT_BUNDLE_IDENTIFIER = com.linhcn.AVCaptureSessionSample; 284 | PRODUCT_NAME = "$(TARGET_NAME)"; 285 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 286 | SWIFT_VERSION = 4.0; 287 | }; 288 | name = Debug; 289 | }; 290 | 87AF5AE71F09DE09000699C1 /* Release */ = { 291 | isa = XCBuildConfiguration; 292 | buildSettings = { 293 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 294 | DEVELOPMENT_TEAM = QXETRBWFB2; 295 | INFOPLIST_FILE = AVCaptureSessionSample/Info.plist; 296 | IPHONEOS_DEPLOYMENT_TARGET = 10.0; 297 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 298 | PRODUCT_BUNDLE_IDENTIFIER = com.linhcn.AVCaptureSessionSample; 299 | PRODUCT_NAME = "$(TARGET_NAME)"; 300 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 301 | SWIFT_VERSION = 4.0; 302 | }; 303 | name = Release; 304 | }; 305 | /* End XCBuildConfiguration section */ 306 | 307 | /* Begin XCConfigurationList section */ 308 | 87AF5ACE1F09DE09000699C1 /* Build configuration list for PBXProject "AVCaptureSessionSample" */ = { 309 | isa = XCConfigurationList; 310 | buildConfigurations = ( 311 | 87AF5AE31F09DE09000699C1 /* Debug */, 312 | 87AF5AE41F09DE09000699C1 /* Release */, 313 | ); 314 | defaultConfigurationIsVisible = 0; 315 | defaultConfigurationName = Release; 316 | }; 317 | 87AF5AE51F09DE09000699C1 /* Build configuration list for PBXNativeTarget "AVCaptureSessionSample" */ = { 318 | isa = XCConfigurationList; 319 | buildConfigurations = ( 320 | 87AF5AE61F09DE09000699C1 /* Debug */, 321 | 87AF5AE71F09DE09000699C1 /* Release */, 322 | ); 323 | defaultConfigurationIsVisible = 0; 324 | defaultConfigurationName = Release; 325 | }; 326 | /* End XCConfigurationList section */ 327 | }; 328 | rootObject = 87AF5ACB1F09DE09000699C1 /* Project object */; 329 | } 330 | -------------------------------------------------------------------------------- /AVCaptureSessionSample.xcodeproj/xcuserdata/linhcn.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /AVCaptureSessionSample.xcodeproj/xcuserdata/linhcn.xcuserdatad/xcschemes/AVCaptureSessionSample.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 | -------------------------------------------------------------------------------- /AVCaptureSessionSample.xcodeproj/xcuserdata/linhcn.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | AVCaptureSessionSample.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 87AF5AD21F09DE09000699C1 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /AVCaptureSessionSample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // AVCaptureSessionSample 4 | // 5 | // Created by Linh Chu on 3/7/17. 6 | // Copyright © 2017 linhcn. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /AVCaptureSessionSample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /AVCaptureSessionSample/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 | -------------------------------------------------------------------------------- /AVCaptureSessionSample/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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /AVCaptureSessionSample/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 | $(PRODUCT_NAME) would like to use camera 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /AVCaptureSessionSample/ViewController.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | import AVFoundation 3 | 4 | class ViewController: UIViewController { 5 | 6 | @IBOutlet weak var capturedImage: UIImageView! 7 | @IBOutlet weak var previewView: UIView! 8 | @IBOutlet weak var photoButton: UIButton! 9 | 10 | let session = AVCaptureSession() 11 | let photoOutput = AVCapturePhotoOutput() 12 | let sessionQueue = DispatchQueue(label: "session queue", 13 | attributes: [], 14 | target: nil) 15 | 16 | var previewLayer : AVCaptureVideoPreviewLayer! 17 | var videoDeviceInput: AVCaptureDeviceInput! 18 | var setupResult: SessionSetupResult = .success 19 | 20 | enum SessionSetupResult { 21 | case success 22 | case notAuthorized 23 | case configurationFailed 24 | } 25 | 26 | override func viewDidLoad() { 27 | super.viewDidLoad() 28 | 29 | checkAuthorization() 30 | 31 | /* 32 | Setup the capture session. 33 | In general it is not safe to mutate an AVCaptureSession or any of its 34 | inputs, outputs, or connections from multiple threads at the same time. 35 | 36 | Why not do all of this on the main queue? 37 | Because AVCaptureSession.startRunning() is a blocking call which can 38 | take a long time. We dispatch session setup to the sessionQueue so 39 | that the main queue isn't blocked, which keeps the UI responsive. 40 | */ 41 | sessionQueue.async { [unowned self] in 42 | self.configureSession() 43 | } 44 | } 45 | 46 | override func viewWillAppear(_ animated: Bool) { 47 | super.viewWillAppear(animated) 48 | 49 | sessionQueue.async { 50 | switch self.setupResult { 51 | case .success: 52 | // Only start the session running if setup succeeded. 53 | DispatchQueue.main.async { [unowned self] in 54 | self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session) 55 | self.previewLayer.frame = self.previewView.bounds 56 | self.previewView.layer.addSublayer(self.previewLayer) 57 | self.session.startRunning() 58 | } 59 | 60 | case .notAuthorized: 61 | DispatchQueue.main.async { [unowned self] in 62 | let changePrivacySetting = "AVCam doesn't have permission to use the camera, please change privacy settings" 63 | let message = NSLocalizedString(changePrivacySetting, comment: "Alert message when the user has denied access to the camera") 64 | let alertController = UIAlertController(title: "AVCam", message: message, preferredStyle: .alert) 65 | 66 | alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Alert OK button"), 67 | style: .cancel, 68 | handler: nil)) 69 | 70 | alertController.addAction(UIAlertAction(title: NSLocalizedString("Settings", comment: "Alert button to open Settings"), 71 | style: .`default`, 72 | handler: { _ in 73 | UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil) 74 | })) 75 | 76 | self.present(alertController, animated: true, completion: nil) 77 | } 78 | 79 | case .configurationFailed: 80 | DispatchQueue.main.async { [unowned self] in 81 | let alertMsg = "Alert message when something goes wrong during capture session configuration" 82 | let message = NSLocalizedString("Unable to capture media", comment: alertMsg) 83 | let alertController = UIAlertController(title: "AVCam", message: message, preferredStyle: .alert) 84 | 85 | alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Alert OK button"), 86 | style: .cancel, 87 | handler: nil)) 88 | 89 | self.present(alertController, animated: true, completion: nil) 90 | } 91 | } 92 | } 93 | } 94 | 95 | override func viewWillDisappear(_ animated: Bool) { 96 | sessionQueue.async { [unowned self] in 97 | if self.setupResult == .success { 98 | self.session.stopRunning() 99 | } 100 | } 101 | 102 | super.viewWillDisappear(animated) 103 | } 104 | 105 | // MARK: Session Management 106 | 107 | func checkAuthorization() { 108 | /* 109 | Check video authorization status. Video access is required and audio 110 | access is optional. If audio access is denied, audio is not recorded 111 | during movie recording. 112 | */ 113 | switch AVCaptureDevice.authorizationStatus(for: AVMediaType.video) { 114 | case .authorized: 115 | // The user has previously granted access to the camera. 116 | break 117 | 118 | case .notDetermined: 119 | /* 120 | The user has not yet been presented with the option to grant 121 | video access. We suspend the session queue to delay session 122 | setup until the access request has completed. 123 | 124 | Note that audio access will be implicitly requested when we 125 | create an AVCaptureDeviceInput for audio during session setup. 126 | */ 127 | sessionQueue.suspend() 128 | AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { [unowned self] granted in 129 | if !granted { 130 | self.setupResult = .notAuthorized 131 | } 132 | self.sessionQueue.resume() 133 | }) 134 | 135 | default: 136 | // The user has previously denied access. 137 | setupResult = .notAuthorized 138 | } 139 | } 140 | 141 | private func configureSession() { 142 | if setupResult != .success { 143 | return 144 | } 145 | 146 | session.beginConfiguration() 147 | session.sessionPreset = AVCaptureSession.Preset.photo 148 | 149 | // Add video input. 150 | do { 151 | var defaultVideoDevice: AVCaptureDevice? 152 | 153 | // Choose the back dual camera if available, otherwise default to a wide angle camera. 154 | let dualCameraDeviceType: AVCaptureDevice.DeviceType 155 | if #available(iOS 11, *) { 156 | dualCameraDeviceType = .builtInDualCamera 157 | } else { 158 | dualCameraDeviceType = .builtInDuoCamera 159 | } 160 | 161 | if let dualCameraDevice = AVCaptureDevice.default(dualCameraDeviceType, for: AVMediaType.video, position: .back) { 162 | defaultVideoDevice = dualCameraDevice 163 | } else if let backCameraDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .back) { 164 | // If the back dual camera is not available, default to the back wide angle camera. 165 | defaultVideoDevice = backCameraDevice 166 | } else if let frontCameraDevice = AVCaptureDevice.default(AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .front) { 167 | /* 168 | In some cases where users break their phones, the back wide angle camera is not available. 169 | In this case, we should default to the front wide angle camera. 170 | */ 171 | defaultVideoDevice = frontCameraDevice 172 | } 173 | 174 | let videoDeviceInput = try AVCaptureDeviceInput(device: defaultVideoDevice!) 175 | 176 | if session.canAddInput(videoDeviceInput) { 177 | session.addInput(videoDeviceInput) 178 | self.videoDeviceInput = videoDeviceInput 179 | 180 | } else { 181 | print("Could not add video device input to the session") 182 | setupResult = .configurationFailed 183 | session.commitConfiguration() 184 | return 185 | } 186 | } catch { 187 | print("Could not create video device input: \(error)") 188 | setupResult = .configurationFailed 189 | session.commitConfiguration() 190 | return 191 | } 192 | 193 | // Add photo output. 194 | if session.canAddOutput(photoOutput) { 195 | session.addOutput(photoOutput) 196 | 197 | photoOutput.isHighResolutionCaptureEnabled = true 198 | photoOutput.isLivePhotoCaptureEnabled = photoOutput.isLivePhotoCaptureSupported 199 | } else { 200 | print("Could not add photo output to the session") 201 | setupResult = .configurationFailed 202 | session.commitConfiguration() 203 | return 204 | } 205 | 206 | session.commitConfiguration() 207 | } 208 | 209 | @IBAction private func capturePhoto(_ sender: UIButton) { 210 | let photoSettings = AVCapturePhotoSettings() 211 | photoSettings.isHighResolutionPhotoEnabled = true 212 | if self.videoDeviceInput.device.isFlashAvailable { 213 | photoSettings.flashMode = .auto 214 | } 215 | 216 | if let firstAvailablePreviewPhotoPixelFormatTypes = photoSettings.availablePreviewPhotoPixelFormatTypes.first { 217 | photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: firstAvailablePreviewPhotoPixelFormatTypes] 218 | } 219 | 220 | photoOutput.capturePhoto(with: photoSettings, delegate: self) 221 | } 222 | } 223 | 224 | // MARK: - AVCapturePhotoCaptureDelegate Methods 225 | 226 | extension ViewController: AVCapturePhotoCaptureDelegate { 227 | 228 | func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { 229 | 230 | if let error = error { 231 | print("Error capturing photo: \(error)") 232 | } else { 233 | if let sampleBuffer = photoSampleBuffer, let previewBuffer = previewPhotoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer) { 234 | 235 | if let image = UIImage(data: dataImage) { 236 | self.capturedImage.image = image 237 | } 238 | } 239 | } 240 | 241 | } 242 | 243 | @available(iOS 11.0, *) 244 | func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { 245 | 246 | guard let data = photo.fileDataRepresentation(), 247 | let image = UIImage(data: data) else { 248 | return 249 | } 250 | 251 | self.capturedImage.image = image 252 | } 253 | } 254 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Linh Chu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AVCamSample 2 | A Swift example of using AVCaptureSession in iOS 10, which has been simplified from [AVCam-iOS](https://developer.apple.com/library/content/samplecode/AVCam/Listings/Swift_AVCam_CameraViewController_swift.html) Apple sample. 3 | --------------------------------------------------------------------------------