iosobjective-ciphonecgaffinetransform

Iphone Rotate video within current frame


Hi I am having a video of width = 720 height = 1280

So video is in Portait mode.

I want the video to be rotate by 90 Degree. BUT want the rotated video inside the same size of video that is 720,1280. Yes video will be scaled , but i need this type of rotation .

Please check Image before and after :

This is mine code :

 CGAffineTransform rotationTransform =  CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(90.0));
    CGAffineTransform rotateTranslate =   CGAffineTransformTranslate(rotationTransform,400,320);
    [layerInst setTransform:rotateTranslate atTime:kCMTimeZero];

Before video screenshot After Video screenshot


Solution

  • You can use scaling, rotation and translation transform together and concat each transform to original asset transform. Please try below code for your question

            // here assetTransform is your AVAssetTrack preferred transform
            CGAffineTransform defaultTransfrom = assetTransform;
            // rotate 90 degree
            CGAffineTransform rotateTransform = CGAffineTransformMakeRotation( M_PI_2);
            //get scale factor of resized video
            float scaleFactor = videoSize.width/videoSize.height;
            CGAffineTransform scaleTransform = CGAffineTransformMakeScale(scaleFactor,scaleFactor);
            // get blank Space At Bottom after rotation and scaling
            float blankSpaceAtBottom = (videoSize.height-scaleFactor*videoSize.width);
            //increase y translation according to scaling factor
            float ytranslation = blankSpaceAtBottom/2/scaleFactor;
            CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(videoSize.height,ytranslation);
            //concat all transform
            CGAffineTransform finalTransform = CGAffineTransformConcat(CGAffineTransformConcat(defaultTransfrom,rotateTransform),translationTransform);
           finalTransform = CGAffineTransformConcat(finalTransform,scaleTransform);
            //apply final transform to layer instruction
            [layerInstruction setTransform:finalTransform atTime:kCMTimeZero];
    

    Please let me know if you need more clarification.