iOS CIImage 旋轉(zhuǎn)

錄制游戲app效果圖.gif

從replaykit2 extension獲取的samplebuffer有3種,分別是RPSampleBufferTypeVideo視頻幀、RPSampleBufferTypeAudioAppapp內(nèi)錄音、RPSampleBufferTypeAudioMic麥克風(fēng),但是從extension獲取的視頻幀尺寸是豎屏的,當(dāng)用戶需要橫屏直播的時(shí)候,我們需要把視頻幀旋轉(zhuǎn)。

以下是旋轉(zhuǎn)的方法:

- (void)dealWithSampleBuffer:(CMSampleBufferRef)buffer {
    CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(buffer);
    
    CIImage *ciimage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
    size_t width                        = CVPixelBufferGetWidth(pixelBuffer);
    size_t height                       = CVPixelBufferGetHeight(pixelBuffer);
   // 旋轉(zhuǎn)的方法
    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);
    
   // 對(duì)新buffer做處理
    
    // release
    CVPixelBufferRelease(newPixcelBuffer);
    CFRelease(newSampleBuffer);
}

對(duì)于CIImage的旋轉(zhuǎn)方法可以使用imageByApplyingCGOrientation: (iOS 11的API) 和 imageByApplyingTransform:

此外,對(duì)于replaykit2返回的samplebuffer有兩種尺寸需要注意,一種是游戲app的幀,尺寸為608 * 1080,還有一種是非游戲app的幀,尺寸為1080 * 1920。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容