Guest User

Untitled

a guest
Aug 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. How to display frames of a Library Video in an UIImageView?
  2. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  3.  
  4.  
  5. [self dismissModalViewControllerAnimated:NO];
  6.  
  7.  
  8. /// incoming video
  9. NSURL *videoURL = [info valueForKey:UIImagePickerControllerMediaURL];
  10. NSLog(@"Video : %@", videoURL);
  11.  
  12. // AVURLAsset to read input movie (i.e. mov recorded to local storage)
  13. NSDictionary *inputOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
  14. AVURLAsset *inputAsset = [[AVURLAsset alloc] initWithURL:videoURL options:inputOptions];
  15.  
  16. // Load the input asset tracks information
  17. [inputAsset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler: ^{
  18.  
  19.  
  20. NSError *error = nil;
  21.  
  22. // Check status of "tracks", make sure they were loaded
  23. AVKeyValueStatus tracksStatus = [inputAsset statusOfValueForKey:@"tracks" error:&error];
  24. if (!tracksStatus == AVKeyValueStatusLoaded)
  25. return;
  26.  
  27.  
  28. AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:inputAsset error:&error];
  29.  
  30. NSMutableDictionary *outputSettings = [NSMutableDictionary dictionary];
  31. [outputSettings setObject: [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey: (NSString*)kCVPixelBufferPixelFormatTypeKey];
  32. AVAssetReaderTrackOutput *readerVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:[[inputAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
  33. outputSettings:outputSettings];
  34.  
  35.  
  36. // Assign the tracks to the reader and start to read
  37. [reader addOutput:readerVideoTrackOutput];
  38. if ([reader startReading] == NO) {
  39. // Handle error
  40. NSLog(@"Error reading");
  41. }
  42.  
  43.  
  44. NSAutoreleasePool *pool = [NSAutoreleasePool new];
  45. while (reader.status == AVAssetReaderStatusReading) {
  46.  
  47. CMSampleBufferRef sampleBufferRef = [readerVideoTrackOutput copyNextSampleBuffer];
  48. if (sampleBufferRef) {
  49. CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBufferRef);
  50.  
  51.  
  52. CVPixelBufferLockBaseAddress(pixelBuffer,0);
  53. /*Get information about the image*/
  54. uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(pixelBuffer);
  55. size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
  56. size_t width = CVPixelBufferGetWidth(pixelBuffer);
  57. size_t height = CVPixelBufferGetHeight(pixelBuffer);
  58.  
  59. NSLog(@"Display Frame : %zu %zu %zu", width, height, bytesPerRow);
  60.  
  61.  
  62. /*We unlock the image buffer*/
  63. CVPixelBufferUnlockBaseAddress(pixelBuffer,0);
  64.  
  65. /*Create a CGImageRef from the CVImageBufferRef*/
  66. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  67. CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
  68.  
  69. UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
  70.  
  71. imageView.image = image;
  72.  
  73. CGImageRelease(newImage);
  74.  
  75. CGContextRelease(newContext);
  76. CGColorSpaceRelease(colorSpace);
  77.  
  78.  
  79.  
  80. CMSampleBufferInvalidate(sampleBufferRef);
  81. CFRelease(sampleBufferRef);
  82. }
  83. }
  84. [pool release];
  85.  
  86. NSLog(@"Finished");
  87. }];
  88.  
  89. }
Add Comment
Please, Sign In to add comment