Square cropping and fixing the video orientation in iOS

前端 未结 3 813
小鲜肉
小鲜肉 2020-12-23 23:55

I am capturing the video using UIImagePickerController, i can crop the video using the following code,

AVAsset *asset = [AVAsset assetWithURL:url];

//create         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-24 00:06

    I know this question is old but some people may still be wondering why some of the videos from the camera roll zoom in after they're cropped. I faced this problem and realized that the cropRect I was using as a frame was not scaled for the different aspect ratios of the video. To fix this problem I simply added the code below to crop the very top of the video into a square. If you want to change the position just change the y value but make sure to scale it according to the video. Luca Iaco provided some great code to get started with. I appreciate it!

    CGSize videoSize = [[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize];
    float scaleFactor;
    
    if (videoSize.width > videoSize.height) {
    
        scaleFactor = videoSize.height/320;
    }
    else if (videoSize.width == videoSize.height){
    
        scaleFactor = videoSize.height/320;
    }
    else{
        scaleFactor = videoSize.width/320;
    }
    
    CGFloat cropOffX = 0;
    CGFloat cropOffY = 0;
    CGFloat cropWidth = 320 *scaleFactor;
    CGFloat cropHeight = 320 *scaleFactor;
    

提交回复
热议问题