Skip to content

Instantly share code, notes, and snippets.

@icanswiftabit
Last active December 11, 2015 07:08
Show Gist options
  • Save icanswiftabit/4564272 to your computer and use it in GitHub Desktop.
Save icanswiftabit/4564272 to your computer and use it in GitHub Desktop.
- (void) readNextMovieFrame
{
AVAssetReaderTrackOutput * output = [_movieReader.outputs objectAtIndex:0];
CMSampleBufferRef sampleBuffer = [output copyNextSampleBuffer];
if (sampleBuffer)
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the image buffer
CVPixelBufferLockBaseAddress(imageBuffer,0);
// Get information of the image
uint8_t *baseAddress __attribute__((unused)) = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow __attribute__((unused)) = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width __attribute__((unused)) = CVPixelBufferGetWidth(imageBuffer);
size_t height __attribute__((unused)) = CVPixelBufferGetHeight(imageBuffer);
//
// Here's where you can process the buffer!
//
/*We unlock the image buffer*/
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
/*Create a CGImageRef from the CVImageBufferRef*/
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
/*We release some components*/
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
/*We display the result on the image view (We need to change the orientation of the image so that the video is displayed correctly)*/
UIImage *image = [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
dispatch_async(dispatch_get_main_queue(), ^{
CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addCGImage:image.CGImage forKey:[NSString stringWithFormat:@"%i",_currentTex2D]];
for (Puzzle *p in _cache) {
[p setTexture: tex];
}
});
CGImageRelease(newImage);
//
// Finish processing the buffer!
//
// Unlock the image buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
CFRelease(sampleBuffer);
/*We relase the CGImageRef*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment