I am trying to crop a video frame. I have recorded the video using AVFoundation
framework. I need to crop the snapped image and recorded video into a square shape. I have done it for the UIImage
which worked, and now I am trying to crop the video.
- (UIImage *)squareImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { // Cropping UIImage is working fine. CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0,0, image.size.width, image.size.width)); UIImage *result = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation]; CGImageRelease(imageRef); return result; }
I tried to crop the video but it resulted in changing the whole video frame without cropping, and it looks stretched.
_writer = [AVAssetWriter assetWriterWithURL:url fileType:AVFileTypeQuickTimeMovie error:nil]; NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys: AVVideoCodecH264, AVVideoCodecKey, [NSNumber numberWithInt: 320], AVVideoWidthKey, [NSNumber numberWithInt: 320], AVVideoHeightKey, nil]; _videoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:settings]; [_writer addInput:_videoInput];
Can anybody help me to solve this cropping issue for video? Thanks in advance.