<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 內存拷貝渲染視頻的研究 這里說的視頻渲染是指通過 `CVPixelBufferRef` 獲取 `CGImageRef` 對象在 UI 上進行渲染的過程。 大家都知道視頻渲染是一個非常麻煩的過程,一般來說我們會通過將 `CVPixelBufferRef` 轉換為 `CIImage` 再將 `CIImage` 對象轉換為 `CGImageRef` 來完成視頻的渲染,其中 `CIImage` 渲染到 `CGImageRef` 的過程將會需要到 `CIContext` 的 `- render:toBitmap:rowBytes:bounds:format:colorSpace:` 方法來實現,但是在實際使用過程中發現,在 iOS 9.0 系統上,使用這個方法渲染視頻時會出現內存泄漏的問題(長時間調試發現似乎是系統的問題)于是花了很多時間來找尋如何繞過 `CIContext` 來進行視頻渲染,最終找到了直接內存拷貝進行視頻渲染的方法,也是最為快速的方法。 ## 代碼解析 下面我們來直接看代碼吧 ``` + (CGImageRef)createImageWithPixelBuffer:(CVPixelBufferRef)pixelBuffer { CGImageRef image = NULL; size_t width = CVPixelBufferGetWidth(pixelBuffer); size_t height = CVPixelBufferGetHeight(pixelBuffer); size_t bytePerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); size_t bitPerCompoment = 8; size_t bitPerPixel = 4 * bitPerCompoment; size_t length = CVPixelBufferGetDataSize(pixelBuffer); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CVPixelBufferLockBaseAddress(pixelBuffer, 0); void *baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer); unsigned char *imageData = (unsigned char *)malloc(length); memcpy(imageData, baseAddress, length); CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); [self.class convertBGRAtoRGBA:imageData withSize:length]; CFDataRef data = CFDataCreate(NULL, imageData, length); free(imageData); CGDataProviderRef provider = CGDataProviderCreateWithCFData(data); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); image = CGImageCreate(width, height, bitPerCompoment, bitPerPixel, bytePerRow, colorSpace, bitmapInfo, provider, NULL, NULL, kCGRenderingIntentDefault); CFRelease(data); CGColorSpaceRelease(colorSpace); CGDataProviderRelease(provider); return image; } ``` 不管是視頻還是圖片,每一幀的畫面都是通過一個 RGBA 或是 BGRA 的位圖來存儲的,所以,實現 `CVPixelBufferRef` 到 `CGImageRef` 的轉換,也就是需要把他們所對應的內存里面保存的位圖數據進行拷貝,來實現圖像的渲染。 上面代碼分為下面部分: - 根據 PixelBuffer 中的信息,以及一些我們已知的信息,獲取創建 `CGImageRef` 對象的必要參數 ``` size_t width = CVPixelBufferGetWidth(pixelBuffer); size_t height = CVPixelBufferGetHeight(pixelBuffer); size_t bytePerRow = CVPixelBufferGetBytesPerRow(pixelBuffer); size_t bitPerCompoment = 8; size_t bitPerPixel = 4 * bitPerCompoment; size_t length = CVPixelBufferGetDataSize(pixelBuffer); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; ``` - 從 PixelBuffer 中拷貝位圖數據,并將位圖數據拷貝到一個 `CFDataRef` 對象中 注意:`[self.class convertBGRAtoRGBA:imageData withSize:length];` 這個方法的目的只是將位圖中的第一位和第三位進行位置交換,因為 PixelBuffer 中的圖像是 BGRA 的,而 CGImage 中的圖像是 RGBA 的。 ``` CVPixelBufferLockBaseAddress(pixelBuffer, 0); void *baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer); unsigned char *imageData = (unsigned char *)malloc(length); memcpy(imageData, baseAddress, length); CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); [self.class convertBGRAtoRGBA:imageData withSize:length]; CFDataRef data = CFDataCreate(NULL, imageData, length); ``` - 通過 `CFDataRef` 創建 `CGImageRef` ``` CGDataProviderRef provider = CGDataProviderCreateWithCFData(data); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); image = CGImageCreate(width, height, bitPerCompoment, bitPerPixel, bytePerRow, colorSpace, bitmapInfo, provider, NULL, NULL, kCGRenderingIntentDefault); CFRelease(data); CGColorSpaceRelease(colorSpace); CGDataProviderRelease(provider); return image; ``` 這樣就實現了視頻的渲染,獲取到視頻的圖像就可以直接渲染到 UI 上了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看