title: Got error in Writing video in document
body: <p>Hi I am capturing screen and make as video. It works well in simulator. While I run it in device. App crashes. below I put my code and error</p>
  
  <pre><code> - (void)stopRecording
      {
  
      [self.displayLink invalidate];
      startTimestamp = 0.0;
      dispatch_async(queue, ^
                     {
  
                         //if (deferImageprocess) {
                             if (self.writer.status != AVAssetWriterStatusCompleted &amp;&amp; self.writer.status != AVAssetWriterStatusUnknown) {
                                 [self.writerInput markAsFinished];
  
                             }
  
                             if ([self.writer respondsToSelector:@selector(finishWritingWithCompletionHandler:)]) {
                                 [self.writer finishWritingWithCompletionHandler:^
                                  {
  
                                      ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
                                      [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:self.pathStr]
                                                                  completionBlock:^(NSURL *assetURL, NSError *error){
                                                                      if (!error)
                                                                      {
                                                                          NSLog(@"Saved Successfully");
                                                                          [self finishBackgroundTask];
                                                                          [self.delegate recordingFinished:[NSString stringWithFormat:@"%@", self.pathStr]];
                                                                      }
                                                                  }];
                                  }];
  
                             }
                             else {
                                 [self.writer finishWriting];
                                 ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
                                 [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:self.pathStr]
                                                             completionBlock:^(NSURL *assetURL, NSError *error){
                                                             }];
                                 [self finishBackgroundTask];
                                 [self restartRecordingIfNeeded];
  
                             }
                          });
  
  }
  
  
  **This above to Wirte the video file in Document as well as gallery**
  
  - (void)captureFrame:(CADisplayLink *)displayLink
      {
          dispatch_async(queue, ^
                         {
                             if (self.writerInput.readyForMoreMediaData) {
                                 CVReturn status = kCVReturnSuccess;
                                 CVPixelBufferRef buffer = NULL;
                                 CFTypeRef backingData;
  
      #if APPSTORE_SAFE || TARGET_IPHONE_SIMULATOR  || TARGET_OS_IPHONE
  
  
                                 __block UIImage *screenshot = nil;
                                 dispatch_sync(dispatch_get_main_queue(), ^{
                                     screenshot = [self screenshot];
                                 });
                                 CGImageRef image = [[self screenshot] CGImage];
                                 CGDataProviderRef dataProvider = CGImageGetDataProvider(image);
                                 CFDataRef data = CGDataProviderCopyData(dataProvider);
                                 backingData = CFDataCreateMutableCopy(kCFAllocatorDefault, CFDataGetLength(data), data);
                                 CFRelease(data);
  
                                 const UInt8 *bytePtr = CFDataGetBytePtr(backingData);
  
                                 status = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
                                                                       CGImageGetWidth(image),
                                                                       CGImageGetHeight(image),
                                                                       kCVPixelFormatType_32BGRA,
                                                                       (void *)bytePtr,
                                                                       CGImageGetBytesPerRow(image),
                                                                       NULL,
                                                                       NULL,
                                                                       NULL,
                                                                       &amp;buffer);
                                 NSLog(@"%d", status);
                                 NSParameterAssert(status == kCVReturnSuccess &amp;&amp; buffer);
      #else
                                 CFTypeRef surface = [UIWindow createScreenIOSurface];
                                 backingData = surface;
  
                                 NSDictionary *pixelBufferAttributes = @{(NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA)};
                                 status = CVPixelBufferCreateWithIOSurface(NULL, surface, (__bridge CFDictionaryRef)(pixelBufferAttributes), &amp;buffer);
                                 NSLog(@"%d", status);
                                 NSParameterAssert(status == kCVReturnSuccess &amp;&amp; buffer);
  
      #endif
                                 if (buffer) {
                                     CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
                                     CFTimeInterval elapsedTime = currentTime - firstFrameTime;
  
                                     CMTime presentTime =  CMTimeMake(elapsedTime * TIME_SCALE, TIME_SCALE);
  
                                     if(![self.writerInputPixelBufferAdaptor appendPixelBuffer:buffer withPresentationTime:presentTime]) {
                                         [self stopRecording];
                                         NSLog(@"Failed");
                                     }
  
                                     CVPixelBufferRelease(buffer);
                                 }
  
                                 CFRelease(backingData);
                             }
                         });
  
          if (startTimestamp == 0.0) {
              startTimestamp = displayLink.timestamp;
          }
  
          NSTimeInterval dalta = displayLink.timestamp - startTimestamp;
  
          if (self.autosaveDuration &gt; 0 &amp;&amp; dalta &gt; self.autosaveDuration) {
              startTimestamp = 0.0;
              [self rotateFile];
          }
      }
  </code></pre>
  
  <p>Above to capture frames.</p>
  
  <p>Now I got this error While stopRecording.</p>
  
  <pre><code>Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVAssetWriterInput markAsFinished] Cannot call method when status is 2'.
  </code></pre>
  
  <p>I don't know what problem here. Please tell me any suggestion</p>