Capture a screenshot of a working video

I have a video that I play using MPMoviePlayerViewController, I want to record a screenshot every 1 second, since I want to perform some action for a separate one present in the captured image.

I use the following code to take a screenshot ----

CGRect contectRect = CGRectMake(0, 0, 1024,768); UIGraphicsBeginImageContext(CGSizeMake(1024,768)); [_player.moviePlayer.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); NSLog(@"view size %f %f",viewImage.size.width,viewImage.size.height); UIGraphicsGetImageFromCurrentImageContext(); CGImageRef imageRef1 = CGImageCreateWithImageInRect([viewImage CGImage], contectRect); UIImage *image = [UIImage imageWithCGImage:imageRef1 scale:1.0orientation:viewImage.imageOrientation]; 

But in this I always get a black image.

I know that there is another way to get an image from a video, for example, the following, but I do not want to use these images, since these images do not meet my requirements.

  AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:path]]; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset]; CMTime time = CMTimeMake(1, 1); UIImage *thumbnail = [UIImage imageWithCGImage:[imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]]; [UIImagePNGRepresentation(thumbnail) writeToFile:imgName atomically:YES]; 
+6
source share
4 answers

You can try to record a thumbnail video. And try to install it.

0
source

look here .

 -(void)generateImage { AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil]; AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; generator.appliesPreferredTrackTransform=TRUE; [asset release]; CMTime thumbTime = CMTimeMakeWithSeconds(0,30); AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){ if (result != AVAssetImageGeneratorSucceeded) { NSLog(@"couldn't generate thumbnail, error:%@", error); } [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal]; thumbImg=[[UIImage imageWithCGImage:im] retain]; [generator release]; }; CGSize maxSize = CGSizeMake(320, 180); generator.maximumSize = maxSize; [generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler]; } 
0
source

You have the ability to request image thumbnails from MPMoviePlayer asynchronously:

 requestThumbnailImagesAtTimes:timeOption: 

But this is a very expensive operation, and there are good chances that you will find it too slow to do much in real time.

Depending on your final goal, you can find much more reliable tools in the GPUImage library , especially since individual frame buffers are easily accessible as CGImageRef objects that you can try periodically.

0
source

Use NSTimer and perform your action accordingly: -

  NSTimer *tm1= [NSTimer scheduledTimerWithTimeInterval:1.0 //here it is 1 seconds target:self selector:@selector(yourMethod:) userInfo:nil repeats:NO]; -(void)yourMethod:(NSTimer*)timer { //your code } 
0
source

Source: https://habr.com/ru/post/956941/


All Articles