How to turn a CVPixelBuffer into a UIImage?

前端 未结 6 1770
梦毁少年i
梦毁少年i 2020-11-28 21:55

I\'m having some problems getting a UIIMage from a CVPixelBuffer. This is what I am trying:

CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(imag         


        
6条回答
  •  孤独总比滥情好
    2020-11-28 22:27

    Unless your image data is in some different format that requires swizzle or conversion - i would recommend no incrementing of anything... just smack the data into your context memory area with memcpy as in:

    //not here... unsigned char *buffer = CVPixelBufferGetBaseAddress(pixelBuffer);
    
    UIGraphicsBeginImageContext(CGSizeMake(w, h));
    
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    void *ctxData = CGBitmapContextGetData(c);
    
    // MUST READ-WRITE LOCK THE PIXEL BUFFER!!!!
    CVPixelBufferLockBaseAddress(pixelBuffer, 0);
    void *pxData = CVPixelBufferGetBaseAddress(pixelBuffer);
    memcpy(ctxData, pxData, 4 * w * h);
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
    
    ... and so on...
    

提交回复
热议问题