How to turn a CVPixelBuffer into a UIImage?

前端 未结 6 1774
梦毁少年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:20

    Another way to get an UIImage. Performs ~10 times faster, at least in my case:

    int w = CVPixelBufferGetWidth(pixelBuffer);
    int h = CVPixelBufferGetHeight(pixelBuffer);
    int r = CVPixelBufferGetBytesPerRow(pixelBuffer);
    int bytesPerPixel = r/w;
    
    unsigned char *buffer = CVPixelBufferGetBaseAddress(pixelBuffer);
    
    UIGraphicsBeginImageContext(CGSizeMake(w, h));
    
    CGContextRef c = UIGraphicsGetCurrentContext();
    
    unsigned char* data = CGBitmapContextGetData(c);
    if (data != NULL) {
       int maxY = h;
       for(int y = 0; y

提交回复
热议问题