Guest User

Untitled

a guest
Mar 13th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #import "CaptureSessionManager.h"
  2.  
  3. @implementation CaptureSessionManager
  4.  
  5. @synthesize captureSession;
  6. @synthesize previewLayer;
  7.  
  8. #pragma mark Capture Session Configuration
  9.  
  10. - (id)init {
  11. if ((self = [super init])) {
  12. [self setCaptureSession:[[AVCaptureSession alloc] init]];
  13. }
  14. return self;
  15. }
  16.  
  17. - (void)addVideoPreviewLayer {
  18. [self setPreviewLayer:[[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]] autorelease]];
  19. [[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
  20.  
  21. }
  22.  
  23. - (void)addVideoInput {
  24. AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  25. if (videoDevice) {
  26. NSError *error;
  27. AVCaptureDeviceInput *videoIn = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
  28. if (!error) {
  29. if ([[self captureSession] canAddInput:videoIn])
  30. [[self captureSession] addInput:videoIn];
  31.  
  32. //else
  33. // NSLog(@"Couldn't add video input");
  34. }
  35.  
  36. // else
  37. // NSLog(@"Couldn't create video input");
  38. }
  39. //else
  40. // NSLog(@"Couldn't create video capture device");
  41. }
  42.  
  43. - (void)dealloc {
  44.  
  45. [[self captureSession] stopRunning];
  46.  
  47. [previewLayer release], previewLayer = nil;
  48. [captureSession release], captureSession = nil;
  49.  
  50. [super dealloc];
  51. }
  52.  
  53. @end
  54.  
  55. [self setCaptureManager:[[CaptureSessionManager alloc] init]];
  56.  
  57. [[self captureManager] addVideoInput];
  58.  
  59. [[self captureManager] addVideoPreviewLayer];
  60. CGRect layerRect = [[[self view] layer] bounds];
  61. [[[self captureManager] previewLayer] setBounds:layerRect];
  62. [[[self captureManager] previewLayer] setPosition:CGPointMake(CGRectGetMidX(layerRect),
  63. CGRectGetMidY(layerRect))];
  64.  
  65. [[[self view] layer] addSublayer:[[self captureManager] previewLayer]];
  66.  
  67. [[captureManager captureSession] startRunning];
  68.  
  69. -(void)viewDidDisappear:(BOOL)animated{
  70. [super viewDidDisappear:YES];
  71.  
  72. [[[self captureManager] previewLayer]removeFromSuperlayer];
  73. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  74. [[captureManager captureSession] stopRunning];
  75. });
  76.  
  77. }
  78.  
  79. [[[self view] layer] addSublayer:[[self captureManager] previewLayer]];
  80.  
  81. -(void)deallocSession
  82. {
  83. [captureVideoPreviewLayer removeFromSuperlayer];
  84. for(AVCaptureInput *input1 in session.inputs) {
  85. [session removeInput:input1];
  86. }
  87.  
  88. for(AVCaptureOutput *output1 in session.outputs) {
  89. [session removeOutput:output1];
  90. }
  91. [session stopRunning];
  92. session=nil;
  93. outputSettings=nil;
  94. device=nil;
  95. input=nil;
  96. captureVideoPreviewLayer=nil;
  97. stillImageOutput=nil;
  98. self.vImagePreview=nil;
  99.  
  100. }
  101.  
  102. [captureSession stopRunning];
  103. for(AVCaptureInput *input in captureSession.inputs) {
  104. [captureSession removeInput:input];
  105. }
  106.  
  107. for(AVCaptureOutput *output in captureSession.outputs) {
  108. [captureSession removeOutput:output];
  109. }
  110.  
  111. func stopRecording() {
  112. captureSession.stopRunning()
  113. for input in captureSession.inputs {
  114. captureSession.removeInput(input)
  115. }
  116. for output in captureSession.outputs {
  117. captureSession.removeOutput(output)
  118. }
  119. }
  120.  
  121. let session = AVCaptureSession()
  122. if let outputMovie = outputMovie, outputMovie.isRecording {
  123. outputMovie.stopRecording()
  124. }
  125.  
  126. self.session.stopRunning()
  127. if let inputs = self.session.inputs as? [AVCaptureDeviceInput] {
  128. for input in inputs {
  129. self.session.removeInput(input)
  130. }
  131. }
  132.  
  133. if let outputs = self.session.outputs as? [AVCaptureOutput] {
  134. for output in outputs {
  135. self.session.removeOutput(output)
  136. }
  137. }
  138.  
  139. session.stopRunning()
  140.  
  141. for(var i = 0 ; i < session.inputs.count ; i++){
  142.  
  143. session.removeInput(session.inputs[i] as! AVCaptureInput)
  144.  
  145. }
  146.  
  147. for(var i = 0 ; i < session.outputs.count ; i++){
  148.  
  149. session.removeOutput(session.outputs[i] as! AVCaptureOutput)
  150.  
  151. }
Add Comment
Please, Sign In to add comment