title: iOS custom camera (take photo), appear a bug
body: <p><a href="https://i.stack.imgur.com/vJpkq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vJpkq.jpg" alt="enter image description here"></a></p>
  
  <p>this bug appears very accidental, I have no idea how to fix, please help me,
  main code is here:</p>
  
  <p>if you want to see the complete project, you can check this link 
  <a href="https://github.com/liman123/custom-camera" rel="nofollow noreferrer">https://github.com/liman123/custom-camera</a></p>
  
  <p>the bug is : when I take photo, the screen become this pic, this pic is distorted!</p>
  
  <pre><code>- (void)setupConfiguration
  {
  _captureSession = [[AVCaptureSession alloc]init];
  _captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
  
  _capturedImageView = [[UIImageView alloc]init];
  _capturedImageView.frame = self.view.frame; // just to even it out
  _capturedImageView.backgroundColor = [UIColor clearColor];
  _capturedImageView.userInteractionEnabled = YES;
  _capturedImageView.contentMode = UIViewContentModeScaleAspectFill;
  
  _captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:_captureSession];
  _captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
  _captureVideoPreviewLayer.frame = self.view.bounds;
  [self.view.layer addSublayer:_captureVideoPreviewLayer];
  
  NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
  if (devices.count &gt; 0) {
      _captureDevice = devices[0];
  
      NSError *error = nil;
      AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:_captureDevice error:&amp;error];
  
      [_captureSession addInput:input];
  
      _stillImageOutput = [[AVCaptureStillImageOutput alloc]init];
      NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
      [_stillImageOutput setOutputSettings:outputSettings];
      [_captureSession addOutput:_stillImageOutput];
  }
  }
  
  -(void)captureButtonClick
  {
  _isCapturingImage = YES;
  AVCaptureConnection *videoConnection = nil;
  for (AVCaptureConnection *connection in _stillImageOutput.connections)
  {
      for (AVCaptureInputPort *port in [connection inputPorts])
      {
          if ([[port mediaType] isEqual:AVMediaTypeVideo] )
          {
              videoConnection = connection;
              break;
          }
      }
      if (videoConnection) {
          break;
      }
  }
  
  [_stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
  
      if (imageSampleBuffer) {
  
          NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
          UIImage *capturedImage = [[UIImage alloc]initWithData:imageData scale:1];
          _isCapturingImage = NO;
          _capturedImageView.image = capturedImage;
          _selectedImage = capturedImage;
          imageData = nil;
  
          [self.view addSubview:_imageSelectedView];
      }
  }];
  }
  </code></pre>