IOS - ReplayKit2 獲取影像方向+ReplayKit的坑

//插件對象
@interface SampleHandler : RPBroadcastSampleHandler



//重寫方法
- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType;


//獲取影像方向信息 
CFStringRef RPVideoSampleOrientationKeyRef = (__bridge CFStringRef)RPVideoSampleOrientationKey;
NSNumber *orientation = (NSNumber *)CMGetAttachment(sampleBuffer, RPVideoSampleOrientationKeyRef,NULL);
NSLog(@"info:%@", orientation);

枚舉

/* Possible int values for kCGImagePropertyTIFFOrientation */
typedef CF_ENUM(uint32_t, CGImagePropertyOrientation) {
    kCGImagePropertyOrientationUp = 1,        // 0th row at top,    0th column on left   - default orientation
    kCGImagePropertyOrientationUpMirrored,    // 0th row at top,    0th column on right  - horizontal flip
    kCGImagePropertyOrientationDown,          // 0th row at bottom, 0th column on right  - 180 deg rotation
    kCGImagePropertyOrientationDownMirrored,  // 0th row at bottom, 0th column on left   - vertical flip
    kCGImagePropertyOrientationLeftMirrored,  // 0th row on left,   0th column at top
    kCGImagePropertyOrientationRight,         // 0th row on right,  0th column at top    - 90 deg CW
    kCGImagePropertyOrientationRightMirrored, // 0th row on right,  0th column on bottom
    kCGImagePropertyOrientationLeft           // 0th row on left,   0th column at bottom - 90 deg CCW
};

直播旋轉角度:

/**獲取影像的方向 計算需旋轉角度   1  ——  */
-(CGFloat)getRotateByBuffer:(CMSampleBufferRef)sampleBuffer{
   CGFloat rotate = 270;
    if (@available(iOS 11.1, *)) {
        /*
         1.1以上支持自動旋轉
         IOS 11.0系統 編譯RPVideoSampleOrientationKey會bad_address
         Replaykit bug:api說ios 11 支持RPVideoSampleOrientationKey 但是 卻存在bad_address的情況 代碼編譯執行會報錯bad_address 即使上面@available(iOS 11.1, *)也無效
         解決方案:Link Binary With Libraries  -->Replaykit  Request-->Option
        */
        CFStringRef RPVideoSampleOrientationKeyRef = (__bridge CFStringRef)RPVideoSampleOrientationKey;
        NSNumber *orientation = (NSNumber *)CMGetAttachment(sampleBuffer, RPVideoSampleOrientationKeyRef,NULL);
        // 如果影像未轉變 獲取到的是nil
        if (orientation == nil && self.lastOrientation) {
            orientation = self.lastOrientation;
        }
        self.lastOrientation = orientation;
        
        switch ([orientation integerValue]) {
                //豎屏時候
                //SDK內部會做圖像大小自適配(不會變形) 所以上層只要控制橫屏時候的影像旋轉的問題
            case kCGImagePropertyOrientationUp:{
                rotate = 0;
            }
                break;
            case kCGImagePropertyOrientationDown:{
                rotate = 180;
                break;
            }
            case kCGImagePropertyOrientationLeft: {
                //靜音鍵那邊向上 所需轉90度
                rotate = 90;
            }
                break;
            case kCGImagePropertyOrientationRight:{
                //關機鍵那邊向上 所需轉270
                rotate = 270;
            }
                break;
            default:
                break;
        }
    }
    return rotate;
}

iOS11.0真機調試異常:

dyld: Symbol not found: _RPVideoSampleOrientationKey

解決方案:

Link Binary With Libraries  -->Replaykit  Request-->Option

備註原文鏈接:https://blog.csdn.net/linpeng_1/article/details/83582521

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章