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];
source share