iOS CIImage 旋转

录制游戏app效果图.gif

从replaykit2 extension获取的samplebuffer有3种,分别是RPSampleBufferTypeVideo视频帧、RPSampleBufferTypeAudioAppapp内录音、RPSampleBufferTypeAudioMic麦克风,但是从extension获取的视频帧尺寸是竖屏的,当用户需要横屏直播的时候,我们需要把视频帧旋转。

以下是旋转的方法:

- (void)dealWithSampleBuffer:(CMSampleBufferRef)buffer {
    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(buffer);
    
    CIImage *ciimage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
    size_t width                        = CVPixelBufferGetWidth(pixelBuffer);
    size_t height                       = CVPixelBufferGetHeight(pixelBuffer);
   // 旋转的方法
    CIImage *wImage = [ciimage imageByApplyingCGOrientation:kCGImagePropertyOrientationLeft];
    
    CIImage *newImage = [wImage imageByApplyingTransform:CGAffineTransformMakeScale(0.5, 0.5)];
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    CVPixelBufferRef newPixcelBuffer = nil;
    CVPixelBufferCreate(kCFAllocatorDefault, height * 0.5, width * 0.5, kCVPixelFormatType_32BGRA, nil, &newPixcelBuffer);
    [_ciContext render:newImage toCVPixelBuffer:newPixcelBuffer];
    
    
    //
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    CMVideoFormatDescriptionRef videoInfo = nil;
    CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, newPixcelBuffer, &videoInfo);
    CMTime duration = CMSampleBufferGetDuration(buffer);
    CMTime presentationTimeStamp = CMSampleBufferGetPresentationTimeStamp(buffer);
    CMTime decodeTimeStamp = CMSampleBufferGetDecodeTimeStamp(buffer);
    CMSampleTimingInfo sampleTimingInfo;
    sampleTimingInfo.duration = duration;
    sampleTimingInfo.presentationTimeStamp = presentationTimeStamp;
    sampleTimingInfo.decodeTimeStamp = decodeTimeStamp;
    //
    CMSampleBufferRef newSampleBuffer = nil;
    CMSampleBufferCreateForImageBuffer(kCFAllocatorMalloc, newPixcelBuffer, true, nil, nil, videoInfo, &sampleTimingInfo, &newSampleBuffer);
    
   // 对新buffer做处理
    
    // release
    CVPixelBufferRelease(newPixcelBuffer);
    CFRelease(newSampleBuffer);
}

对于CIImage的旋转方法可以使用imageByApplyingCGOrientation: (iOS 11的API) 和 imageByApplyingTransform:

此外,对于replaykit2返回的samplebuffer有两种尺寸需要注意,一种是游戏app的帧,尺寸为608 * 1080,还有一种是非游戏app的帧,尺寸为1080 * 1920

你可能感兴趣的:(iOS CIImage 旋转)