将UIView图层转换为UIImage

我在视图中使用AVPlayerLayer播放video。 我需要将视图转换成图像,我试过了

[myview.layer renderInContext:context]; 

但是这只给出黑色的图像。 我想在那个时候把视图转换成video。 这个转换将同时发生0.05s。

我试着用AVAssetImageGenerator。 这使我使用资产的正确形象。 但是这花了很多时间,这使得我的应用程序出现了一些性能问题。 任何人都可以帮助我如何减less特定的CMTimevideo转换为图像的过程。

以下是我的编码。

 - (UIImage *)currentItemScreenShot { AVPlayer *abovePlayer = [objVC player]; if(imageGenerator == nil) { AVAsset *asset = [[[objVC player] currentItem] asset]; imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; } CMTime time = [[abovePlayer currentItem] currentTime]; if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) { [imageGenerator setRequestedTimeToleranceBefore:kCMTimeZero]; [imageGenerator setRequestedTimeToleranceAfter:kCMTimeZero]; } CGImageRef imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; if (imgRef == nil) { if ([imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceBefore:)] && [imageGenerator respondsToSelector:@selector(setRequestedTimeToleranceAfter:)]) { [imageGenerator setRequestedTimeToleranceBefore:kCMTimePositiveInfinity]; [imageGenerator setRequestedTimeToleranceAfter:kCMTimePositiveInfinity]; } imgRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL]; } UIImage *image = [UIImage imageWithCGImage:imgRef]; CGImageRelease(imgRef); image = [self reverseImageByScalingToSize:image.size :image]; return image; } 

MPMoviePlayerController可以很容易地从电影中的特定位置获取影像。

  - (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; } 

只要调用这个video的path,并在当时你想要的图像。

请尝试下面的代码。 这对我来说是完美的。

并参考UIKit函数参考 。

 + (UIImage *) imageWithView:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } 

也试试这个

你可以把你的UIView保存为Document Diretory中的Image,如下所示:

 -(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; } 

此代码当用户想要捕捉当前视图ScreenShot和共享或保存此图像有用….

 - (UIImage *)captureView { //hide controls if needed CGRect rect = [self.view bounds]; UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [self.view.layer renderInContext:context]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } 

另请参阅我的这个答案…. 我的答案

更新:

 AVPlayerItem *item = [AVPlayerItem playerItemWithURL:yourURL]; AVPlayer *player = [AVPlayer playerWithPlayerItem:pItem]; //observe 'status' [playerItem addObserver:self forKeyPath:@"status" options:0 context:nil]; - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"status"]) { AVPlayerItem *item = (AVPlayerItem *)object; if (item.status == AVPlayerItemStatusReadyToPlay) { AVURLAsset *asset = (AVURLAsset *)item.asset; AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset]; CGImageRef thumb = [imageGenerator copyCGImageAtTime:CMTimeMakeWithSeconds(10.0, 1.0) actualTime:NULL error:NULL]; } } } 

我的项目之一,我使用下面的代码来播放video的图像也许这也帮助你…看看,并尝试调整你的代码。

 #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; 

}

尝试截图和裁剪。 否则,您可能需要recursion地呈现视图及其所有子视图。

 -(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; }