Here's how you create an NSData object from a CMSampleBufferRef:
In the interface (.h) file, add the sample buffer reference as a property, casting to an object at the same time:
@property (nonatomic, strong) __attribute__((NSObject)) CMSampleBufferRef sampleBuffer;
In the implementation file (.m):
CMSampleBufferRef sampleBuffer = (CMSampleBufferRef)[(AVAssetReaderTrackOutput *)[assetReader outputs][0] copyNextSampleBuffer];
NSPurgeableData *sampleBufferData = (NSPurgeableData *)[self imageToBuffer:sampleBuffer];
To access the sample buffer from the NSData object, simply use a cast:
(CMSampleBufferRef)sampleBufferData;
Here's another way:
- (NSData *) imageToBuffer:(CMSampleBufferRef)source {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(source);
CVPixelBufferLockBaseAddress(imageBuffer,0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
void *src_buff = CVPixelBufferGetBaseAddress(imageBuffer);
NSData *data = [NSData dataWithBytes:src_buff length:bytesPerRow * height];
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
return data;
}