AVFoundation exporting orientation wrong

前端 未结 2 1800
不思量自难忘°
不思量自难忘° 2021-02-03 10:29

I\'m attempting to combine an image and a video. I have them combining and exporting however it\'s rotated side ways.

Sorry for the bulk code paste. I\'ve seen answers

2条回答
  •  攒了一身酷
    2021-02-03 11:21

    Maybe U should check the videoTrack's preferredTransform so to give it a exact renderSize and transform:

    CGAffineTransform transform = assetVideoTrack.preferredTransform;
    CGFloat rotation = [self rotationWithTransform:transform]; 
    //if been rotated
            if (rotation != 0)
            {
                //if rotation is 360°
                if (fabs((rotation - M_PI * 2)) >= valueOfError) {
    
                    CGFloat m = rotation / M_PI;
                    CGAffineTransform t1;
                    //rotation is 90° or 270°
                    if (fabs(m - 1/2.0) < valueOfError || fabs(m - 3/2.0) < valueOfError) {
                        self.mutableVideoComposition.renderSize = CGSizeMake(assetVideoTrack.naturalSize.height,assetVideoTrack.naturalSize.width);
                        t1 = CGAffineTransformMakeTranslation(assetVideoTrack.naturalSize.height, 0);
                    }
                    //rotation is 180°
                    if (fabs(m - 1.0) < valueOfError) {
                        t1 = CGAffineTransformMakeTranslation(assetVideoTrack.naturalSize.width, assetVideoTrack.naturalSize.height);
                    }
                    CGAffineTransform t2 = CGAffineTransformRotate(t1,rotation);
                    //                CGAffineTransform transform = makeTransform(1.0, 1.0, 90, videoTrack.naturalSize.height, 0);
                    [passThroughLayer setTransform:t2 atTime:kCMTimeZero];
                }
            }
    
    //convert transform to radian
    - (CGFloat)rotationWithTransform:(CGAffineTransform)t
    {
        return atan2f(t.b, t.a);
    }
    

提交回复
热议问题