因为需要在相机界面实时缩放,效率原因,不采取CMSampleBufferRef转cgimage在缩放。
暂时找到的方法是通过vImage来缩放,代码如下,缩放成功返回得到的CVImageBuffer,转化为uiimage可以显示到imageView,却无法转化为CVMetalTextureRef(因为需要对缩放的CVImageBuffer进行滤镜操作)。
其他方式 https://www.jianshu.com/p/ac79a80f1af2
CVImageBufferRef imageBuffer =CMSampleBufferGetImageBuffer(sampleBuffer);
vImage_Error ret;
// Create dest buffer
constNSUIntegerscale =1;
constNSUIntegerdstWidth = (NSUInteger)fitSize.width* scale;
constNSUIntegerdstHeight = (NSUInteger)fitSize.height* scale;
constNSUIntegerbytesPerPixel =4;
constNSUIntegerdstBytesPerRow = bytesPerPixel * dstWidth;
uint8_t* dstData = (uint8_t*)calloc(dstHeight * dstWidth * bytesPerPixel,sizeof(uint8_t));
vImage_BufferdstBuffer = {
.data= dstData,
.height= dstHeight,
.width= dstWidth,
.rowBytes= dstBytesPerRow,
};
// Scale
ret =vImageScale_ARGB8888(&srcBuffer, &dstBuffer, NULL, kvImageHighQualityResampling);
// free(srcBuffer.data);
if (ret != kvImageNoError)
{
free(dstData);
returnnil;
}
CVPixelBufferRef pxbuffer =NULL;
NSDictionary *options =@{(NSString *)kCVPixelBufferCGImageCompatibilityKey:@YES,(NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey:@YES,(NSString *)kCVPixelBufferMetalCompatibilityKey:@YES};
CVReturnstatus =CVPixelBufferCreateWithBytes(kCFAllocatorDefault, fitSize.width, fitSize.height,kCVPixelFormatType_32BGRA, dstData, dstBuffer.rowBytes,nil,nil, (__bridgeCFDictionaryRef)options, &pxbuffer);
return pxbuffer;
滤镜操作无法完成,代码如下
CVMetalTextureRef thumbMetalTexture;
CVReturnssstatus = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault,self.thumbTextureCache, thttt,NULL,MTLPixelFormatBGRA8Unorm,50,50,0, &thumbMetalTexture);
if(ssstatus !=0) {
NSLog(@"%d",ssstatus);
return;
}
ssstatus = -6660,原因未知。
网友评论