0

我正在尝试开发广播扩展来进行屏幕录制。我的路径是从扩展中获取 CMSampleBufferRef,检查类型是否是视频转换为 UIImage,然后通过 MMWormhole 与容器应用程序共享。到目前为止,我可以做到。但是扩展在样本缓冲区到 UIImage 转换期间随机崩溃。我不知道为什么。

这是我的 CMSampleBufferRef 到 UIImage 的转换函数

-(void) convertAndSend:(CMSampleBufferRef)sampleBuffer {


    dispatch_async(dispatch_get_main_queue(), ^{

            // Get a CMSampleBuffer's Core Video image buffer for the media data
            CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

            // Lock the base address of the pixel buffer
            CVPixelBufferLockBaseAddress(imageBuffer, 0);

            // Get the number of bytes per row for the pixel buffer
            void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);


            // Get the pixel buffer width and height
            size_t width = CVPixelBufferGetWidth(imageBuffer);
            size_t height = CVPixelBufferGetHeight(imageBuffer);


            // Create a device-dependent RGB color space
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            NSUInteger bytesPerRow = 4 * width;
            NSUInteger bitsPerComponent = 8;

            // Create a bitmap graphics context with the sample buffer data
            CGContextRef context = CGBitmapContextCreate(NULL,width,height,bitsPerComponent,bytesPerRow, colorSpace,kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
            // Create a Quartz image from the pixel data in the bitmap graphics context
            CGImageRef quartzImage = CGBitmapContextCreateImage(context);
            // Unlock the pixel buffer
            CVPixelBufferUnlockBaseAddress(imageBuffer,0);

            // Free up the context and color space
            CGContextRelease(context);
            CGColorSpaceRelease(colorSpace);

            // Create an image object from the Quartz image
            //UIImage *image = [UIImage imageWithCGImage:quartzImage];
            UIImage *image = [UIImage imageWithCGImage:quartzImage
                                     scale:1.0f
                                   orientation:UIImageOrientationRight];

            // Release the Quartz image
            CGImageRelease(quartzImage);

            if (image) {
                NSString *imgData = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

                [self.wormhole passMessageObject:@{@"data" : imgData} identifier:@"streamData"];
            }
    });  
}

通常扩展在 CVPixelBufferUnlockBaseAddress 上崩溃。因为 EXC_BAD_ACCESS(代码=1,地址=0x0)

任何想法或帮助?

谢谢。

4

0 回答 0