It seems that I got some improvement.
In function (CVPixelBufferRef) newPixelBufferFromCGImage: (CGImageRef) image
the change:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
in
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, [NSNumber numberWithInt:4*frameSize.width], kCVPixelBufferBytesPerRowAlignmentKey,nil];
The photo is not distorted . but apparently it scales in its y-string to 0.5 as its normal height!
I am still working on a solution to this problem.
update:
I completely solved this problem: This is because the transform attribute and preformTransform
The transform specified in the track's storage container as the preferred
conversion of visual media for display purposes.
This means that the actual orientation of the video file may not be the same as you see during playback.
AVPlayer uses the conversion in this file for playback. However, generateCGImagesAsynchronouslyForTimes ignores this attribute. and get a landscape (for example). therefore, you will need to set the conversion back to the original video file.
Just add it before.
writerInput.transform = CGAffineTransformMakeRotation(M_PI_2) ;
front
AVAssetWriterInput* writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] ;
will solve this problem.