2013-03-09 229 views
6

我想将AVPlayerItemVideoOutput中提取的pixelBuffer转换为CIImage,但总是得到nil。CVPixelBuffer到CIImage总是返回零

守则

if([videoOutput_ hasNewPixelBufferForItemTime:player_.internalPlayer.currentItem.currentTime]) 
{ 
    CVPixelBufferRef pixelBuffer = [videoOutput_ copyPixelBufferForItemTime:player_.internalPlayer.currentItem.currentTime 
                 itemTimeForDisplay:nil]; 

    CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer]; // Always image === nil 
    CIFilter *filter = [FilterCollection filterSepiaForImage:image]; 
    image = filter.outputImage; 

    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef cgimg = [context createCGImage:image fromRect:[image extent]]; 
    [pipLayer_ setContents:(id)CFBridgingRelease(cgimg)]; 

} 

下面是为了创造一个CIImage(其结果总是在无)使用的pixelBuffer的细节:

$0 = 0x09b48720 <CVPixelBuffer 0x9b48720 width=624 height=352 bytesPerRow=2496 pixelFormat=BGRA iosurface=0x0 attributes=<CFBasicHash 0x98241d0 [0x1d244d8]>{type = immutable dict, count = 3, 
entries => 
    0 : <CFString 0x174cf4 [0x1d244d8]>{contents = "Height"} = <CFNumber 0x9a16e70 [0x1d244d8]>{value = +352, type = kCFNumberSInt32Type} 
    1 : <CFString 0x174ce4 [0x1d244d8]>{contents = "Width"} = <CFNumber 0x9a109d0 [0x1d244d8]>{value = +624, type = kCFNumberSInt32Type} 
    2 : <CFString 0x1750e4 [0x1d244d8]>{contents = "PixelFormatType"} = <CFArray 0x1090ddd0 [0x1d244d8]>{type = mutable-small, count = 1, values = (
    0 : <CFNumber 0x7a28050 [0x1d244d8]>{value = +1111970369, type = kCFNumberSInt32Type} 
)} 
} 
propagatedAttachments=<CFBasicHash 0x9b485c0 [0x1d244d8]>{type = mutable dict, count = 6, 
entries => 
    0 : <CFString 0x174ff4 [0x1d244d8]>{contents = "CVImageBufferTransferFunction"} = <CFString 0x174f84 [0x1d244d8]>{contents = "ITU_R_709_2"} 
    2 : <CFString 0x174f74 [0x1d244d8]>{contents = "CVImageBufferYCbCrMatrix"} = <CFString 0x174f94 [0x1d244d8]>{contents = "ITU_R_601_4"} 
    9 : <CFString 0x174f14 [0x1d244d8]>{contents = "CVPixelAspectRatio"} = <CFBasicHash 0x9b1bc30 [0x1d244d8]>{type = immutable dict, count = 2, 
entries => 
    0 : <CFString 0x174f34 [0x1d244d8]>{contents = "VerticalSpacing"} = <CFNumber 0x9b0f730 [0x1d244d8]>{value = +1, type = kCFNumberSInt32Type} 
    2 : <CFString 0x174f24 [0x1d244d8]>{contents = "HorizontalSpacing"} = <CFNumber 0x9b0f730 [0x1d244d8]>{value = +1, type = kCFNumberSInt32Type} 
} 

    10 : <CFString 0x174fb4 [0x1d244d8]>{contents = "CVImageBufferColorPrimaries"} = <CFString 0x174fd4 [0x1d244d8]>{contents = "SMPTE_C"} 
    11 : <CFString 0x174e24 [0x1d244d8]>{contents = "QTMovieTime"} = <CFBasicHash 0x7a47940 [0x1d244d8]>{type = immutable dict, count = 2, 
entries => 
    0 : <CFString 0x174e44 [0x1d244d8]>{contents = "TimeScale"} = <CFNumber 0x7a443d0 [0x1d244d8]>{value = +90000, type = kCFNumberSInt32Type} 
    2 : <CFString 0x174e34 [0x1d244d8]>{contents = "TimeValue"} = <CFNumber 0x7a476e0 [0x1d244d8]>{value = +1047297, type = kCFNumberSInt64Type} 
} 

    12 : <CFString 0x174eb4 [0x1d244d8]>{contents = "CVFieldCount"} = <CFNumber 0x9b0f730 [0x1d244d8]>{value = +1, type = kCFNumberSInt32Type} 
} 
nonPropagatedAttachments=<CFBasicHash 0x9b44b40 [0x1d244d8]>{type = mutable dict, count = 0, 
entries => 
} 
> 

回答

10

解决这个问题的过程中与其他键一起本词典 - 我是想在模拟器,但在我看来这仅在设备上受支持。

+0

OMG ...是否记录在某处? – 2014-10-21 18:56:09

+0

这也“解决了”我的问题。在Swift中,我让inputImage = CIImage(CVPixelBuffer:pixelBuffer)总是在模拟器上返回nil,并在设备上返回一个对象。 :( – 2014-11-17 22:56:47

+0

即使在设备上我也没有得到零。任何人都可以说出原因吗? – 2016-04-01 07:25:08

2

看来你没有定义了iosurface属性键。不是100%它会解决你的问题,但试试这个:

CFDictionaryRef attrs = (CFDictionaryRef)@{ 
    (id)kCVPixelBufferWidthKey: @(WIDTH_OF_YOUR_VIDEO_GOES_HERE), 
    (id)kCVPixelBufferHeightKey: @(HEIGHT_OF_YOUR_VIDEO_GOES_HERE), 
    (id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;), // kCVPixelFormatType_32BGRA 
    (id)kCVPixelBufferIOSurfacePropertiesKey: @{}, 
} 

而且通过创造您的pixelbuffer

+0

在macos v10.10中,必须设置kCVPixelBufferPixelFormatTypeKey = kCVPixelFormatType_32ARGB,然后'+ [CIImage imageWithCVPixelBuffer:]'可以成功 – fengxing 2016-04-28 12:04:45

0

这不是您设置图层内容的方式;这是:

(__bridge id)uiImage.CGImage;

你没有办法让你在设备或模拟器上工作。