ios - 如何使用Metal API或Accelerate Framework绘制裁剪后的位图?

标签 ios core-graphics accelerate-framework metal vimage

我正在实现裁剪视频帧的自定义视频合成器。目前,我使用Core Graphics来做到这一点:

-(void)renderImage:(CGImageRef)image inBuffer:(CVPixelBufferRef)destination {
    CGRect cropRect = // some rect ...
    CGImageRef currentRectImage = CGImageCreateWithImageInRect(photoFullImage, cropRect);

    size_t width = CVPixelBufferGetWidth(destination);
    size_t height = CVPixelBufferGetHeight(destination);

    CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(destination),       // data
                                             width,
                                             height,
                                             8,                                              // bpp
                                             CVPixelBufferGetBytesPerRow(destination),
                                             CGImageGetColorSpace(backImage),
                                             CGImageGetBitmapInfo(backImage));

    CGRect frame = CGRectMake(0, 0, width, height);
    CGContextDrawImage(context, frame, currentRectImage);
    CGContextRelease(context);
}

我如何使用Metal API来做到这一点?它应该快得多,对吗?
使用Accelerate Framework(特别是vImage)怎么样?会更简单吗?

最佳答案

好的,我不知道这对您是否有用,但仍然如此。
查看以下代码:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
  CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

  id<MTLTexture> textureY = nil;

  {
    size_t width = CVPixelBufferGetWidth(pixelBuffer);
    size_t height = CVPixelBufferGetHeight(pixelBuffer);

    MTLPixelFormat pixelFormat = MTLPixelFormatBGRA8Unorm;

    CVMetalTextureRef texture = NULL;
    CVReturn status = CVMetalTextureCacheCreateTextureFromImage(NULL, _textureCache, pixelBuffer, NULL, pixelFormat, width, height, 0, &texture);
    if(status == kCVReturnSuccess)
    {
      textureY = CVMetalTextureGetTexture(texture);
      if (self.delegate){
        [self.delegate textureUpdated:textureY];
      }
      CFRelease(texture);
    }
  }
}

我使用此代码将CVPixelBufferRef转换为MTLTexture。之后,您可能应该创建blitCommandEncoder并使用它
func copyFromTexture(sourceTexture: MTLTexture, sourceSlice: Int, sourceLevel: Int, sourceOrigin: MTLOrigin, sourceSize: MTLSize, toTexture destinationTexture: MTLTexture, destinationSlice: Int, destinationLevel: Int, destinationOrigin: MTLOrigin)

在其中,您可以选择裁剪的矩形并将其复制到其他纹理。

下一步是将生成的MTLTextures转换回CVPixelBufferRefs,然后从中制作视频,很遗憾,我不知道该怎么做。

真的很想听听您的想法。干杯。

关于ios - 如何使用Metal API或Accelerate Framework绘制裁剪后的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812814/

相关文章:

ios - 如何通过加密保护文档目录中的数据?

ios - 强制本地化 DateComponentsFormatter 中的 allowedUnits

ios - 如何创建带有 DPI 集的 UIImage?

iOS - 使用 Accelerate.framework 计算矩阵每一行和/或列的点积

ios - 章节标题 Tableview iOS 上的操作

带边框的 iOS UIImage 有一个粗角半径

ios - 在iPad上,为了保留当前主视图的位图,Core Graphics与cocos2D和OpenGL ES相比如何?

ios - FFT的大小实际上意味着什么

ios - 如何转换 UnsafeMutableRawPointer!到 Swift 3.0 或更新版本中的 UnsafeMutablePointer<Float>?

ios - 在 Swift 中删除/排除枚举案例