I\'m playing Video using AVPlayerLayer in a View. I need to convert View to Image, I tried
[myview.layer renderInContext:context];
but thi
The MPMoviePlayerController makes it easy to get a image from a movie at a certain point in the movie.
- (UIImage*)imageFromVideoAtPath:(NSString *)path atTime:(NSTimeInterval)time {
NSURL *videoURL = [NSURL fileURLWithPath:path];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer prepareToPlay];
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
[moviePlayer stop];
return thumbnail;
}
Just call this with the path of the video, and at the time you want to get the image.
Try taking a screenshot and clipping. Otherwise you may have to render view and all its subviews recursively.
-(UIImage *)videoScreenCap:(CGRect)cropRect{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
UIGraphicsBeginImageContext(self.window.bounds.size);
[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * data = UIImagePNGRepresentation(image);
[data writeToFile:@"foo.png" atomically:YES];
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
UIImage * img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}
you can save your UIView as Image in Document Diretory like:
-(IBAction)ViewTOimage:(id)sender
{
UIGraphicsBeginImageContext(self.view.bounds.size); //instad of self.view you can set your view IBOutlet name
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(saveImage);
NSFileManager *fileMan = [NSFileManager defaultManager];
NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];
}
-(UIImage *)imagefromview {
UIGraphicsBeginImageContext(view.frame.size);
[[self imageFromView:view] drawInRect:view.frame];
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
I one of the project i used the below code to take an image from playing video might this help you too... take a look and try adjusting the code with your's.
#import <AVFoundation/AVFoundation.h>
@property (nonatomic,retain) AVCaptureStillImageOutput * resultImageOutput;
- (UIImage*)stillImageFromVideo
{
AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in [[self resultImageOutput] connections]) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
videoConnection = connection;
break;
}
}
if (videoConnection) {
break;
}
}
[[self resultImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer,
kCGImagePropertyExifDictionary, NULL);
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
return image;
}
}
Please try below code. It is working perfectly for me.
And refer UIKit Function Reference.
+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
Also try this.