Można utworzyć dostawcę danych z danych adresu bazowego bez kopiowania, a następnie utworzyć obiekt UIImage od tego dostawcy danych. Aby uniknąć ponownego użycia bufora podczas przywoływania tego obrazu, musisz zachować bufor próbek i zablokować adres bazowy. Powinny one zostać odblokowane i zwolniony automatycznie, kiedy użytkownik zapomni tego obiektu obrazu:
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection
{
// Retain sample buffer and lock base address
CFRetain(sampleBuffer);
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
void *baseAddress = (void *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
UIImage *image = imageFromData(baseAddress, width, height, bytesPerRow, sampleBuffer);
// Now you can store this UIImage as long as you want
}
ja mam imageFromData
z tego projektu https://github.com/k06a/UIImage-DecompressAndMap/blob/master/UIImage%2BDecompressAndMap.m i przyjęła go trochę:
UIImage *imageFromData(void *data, size_t width, size_t height, size_t bytesPerRow, CMSampleBufferRef sampleBuffer)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData((void *)sampleBuffer, data, bytesPerRow * height, munmap_wrapper);
CGImageRef inflatedImage = CGImageCreate(width, height, 8, 4*8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst, provider, NULL, NO, kCGRenderingIntentDefault);
CGColorSpaceRelease(colorSpace);
CGDataProviderRelease(provider);
UIImage *img = [UIImage imageWithCGImage:inflatedImage scale:scale orientation:UIImageOrientationUp];
CGImageRelease(inflatedImage);
return img;
}
trzeba także zapewnić unlock_function
:
void unlock_function(void *info, const void *data, size_t size)
{
// Unlock base address release sample buffer
CMSampleBufferRef sampleBuffer = (CMSampleBufferRef)info;
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
CFRelease(sampleBuffer);
}
Cześć Hammer, jak renderujesz swój obraz cv :: Mat w OpenGL ES, proszę? Z góry dziękuję – lilouch