title: getting unexpected result of crop video using AVFoundation
body: <p>I have video of 1920*1080(16:9) and I want to crop that video by middle pixel of 1080*1080(1:1).
  <br/>For that I wrote below code.</p>
  
  <pre><code>-(void)cropVideo
  {
      AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
      AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
      AVMutableCompositionTrack *track1 = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
      NSString *path = [NSString stringWithFormat:@"%@/staticVideo.mp4", [[NSBundle mainBundle] resourcePath]];
      AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:path]];
  
      [track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:CMTimeMake(0, 1) error:nil];
      [track1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMake(0, 1) error:nil];
  
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                               [NSString stringWithFormat:@"%@1x1.mov",NSBundle.mainBundle.infoDictionary[@"CFBundleExecutable"]]];
      [[NSFileManager defaultManager] removeItemAtPath:myPathDocs error:nil];
      NSURL *url = [NSURL fileURLWithPath:myPathDocs];
  
      /** code for crop video*/
  
      AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
      instruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
  
      AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:track];
  
      [layerInstruction setCropRectangle:CGRectMake(420, 0, 1080, 1080) atTime:kCMTimeZero];
  
      [layerInstruction setOpacity:0.0 atTime:asset.duration];
  
      instruction.layerInstructions = [NSArray arrayWithObjects:layerInstruction,nil];
  
      AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
  
      mainCompositionInst.renderSize = CGSizeMake(1080, 1080);
      mainCompositionInst.instructions = [NSArray arrayWithObject:instruction];
      mainCompositionInst.frameDuration = CMTimeMake(1, 30);
  
      /** code for crop video*/
  
      AVAssetExportSession *exporter;
      int currentSize = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"VideoSize"];
      if (currentSize == 1)
      {
          exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];
      }
      else if (currentSize == 2)
      {
          exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
      }
      else if (currentSize == 3)
      {
          exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1920x1080];
      }
      else if (currentSize == 4)
      {
          exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset3840x2160];
      }
      exporter.videoComposition = mainCompositionInst;
      exporter.outputURL=url;
      exporter.outputFileType = AVFileTypeQuickTimeMovie;
      exporter.shouldOptimizeForNetworkUse = YES;
  
      [exporter exportAsynchronouslyWithCompletionHandler:^{
          dispatch_async(dispatch_get_main_queue(), ^{
              /*update Video*/
          });
      }];
  
  }
  </code></pre>
  
  <p>Sorry, I can't explain how I want and how I'm getting.
  <br/>So I'm drew diagram from this you can understand my problem.</p>
  
  <p><a href="https://i.stack.imgur.com/Ar50X.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ar50X.jpg" alt="diagram"></a>
  <br/><br/>Like this diagram I want 1080*1080 of A result but I'm getting 1080*1080 of B result.
  Here is the screenshot how result B is appearing.<br/><br/>
  <a href="https://i.stack.imgur.com/xZPYQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xZPYQ.png" alt="ScreenShot of B result"></a></p>
  
  <p>Please help me to solve this problem.
  Thanks in advance.</p>