iOS 視頻截取
阿新 • • 發佈:2017-12-20
ecg mtime hicsg () after nsurl max nsstring 大量
最近開發一個app,需要大量用到這方面的知識,於是寫了這一個文檔,希望能幫上一些需要查閱的小夥伴,基本上都是代碼片段,沒有什麽概念知識。
截取圖片的一部分
-(UIImage *)getPartOfImage:(UIImage *)img rect:(CGRect)partRect { CGImageRef imageRef = img.CGImage; CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, partRect); UIImage *retImg = [UIImage imageWithCGImage:imagePartRef]; CGImageRelease(imagePartRef);return retImg; }
ios屏幕截圖
普通截圖不能截取通過openGl渲染的視頻,所以截取視頻是不能使用下面的方法的,需要小夥伴自己去尋找其他openGL的截圖方法了
UIGraphicsBeginImageContextWithOptions(_videoRect.size, YES, 0); //設置截屏大小 [[_moviePlayerController.view layer] renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGImageRef imageRef = viewImage.CGImage; // CGRect rect = CGRectMake(166, 211, 426, 320);//這裏可以設置想要截圖的區域 CGRect rect = _videoRect;//這裏可以設置想要截圖的區域 CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect); UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect]; UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存圖片到照片庫 NSData *imageViewData = UIImagePNGRepresentation(sendImage); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *pictureName= [NSString stringWithFormat:@"screenShow_%d.png",2]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName]; NSLog(@"截屏路徑打印: %@", savedImagePath); [imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目錄 CGImageRelease(imageRefRect);
視頻截圖一
ASSet中有兩個方法可以截圖,下面的方法就是其中一個,相比於(視頻截圖二)那個方法,這一個要快得多
但是我不能把控他得很多細節,所以推薦使用(視頻截圖三)
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@",urlstring]];//initFileURLWithPath:videoURL] autorelease]; AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:nil]; AVAssetImageGenerator*generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset]; generator.appliesPreferredTrackTransform = NO; generator.maximumSize = _videoRect.size; NSError *error = nil; for (float i = 0; i < second ; i += 0.1) { //不會CMTime就去了解一下 CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(i, 10) actualTime:NULL error:&error]; UIImage *image = [UIImage imageWithCGImage: img]; [imageArray addObject:image]; } NSLog(@"iamgeArray = %@",imageArray); return imageArray;
視頻截圖二
iOS 9.0之後就不使用下面的方法了,如果大量截圖的話建議不要使用這種方法,很慢,但是有點就是MPMovie中的時間就是我們平時的時間 1 秒對應視頻中得 第1秒、兩秒對應視頻中得第2秒 ...,而且幾句代碼就搞定了
NSMutableArray *imageArray = [NSMutableArray array]; MPMoviePlayerViewController * mpviemController =[[MPMoviePlayerViewController alloc] initWithContentURL:urlstring]; MPMoviePlayerController *mp=[mpviemController moviePlayer]; for (float i = 0; i < second; i += 0.1) { UIImage *thumbImage=[mp thumbnailImageAtTime:i timeOption:MPMovieTimeOptionExact]; //MPMovieTimeOptionExact截取每一幀的圖片,MPMovieTimeOptionNearestKeyFrame關鍵幀 NSData *imagedata =UIImagePNGRepresentation(thumbImage); UIImage *image = [UIImage imageWithData:imagedata]; [imageArray addObject:image]; } return imageArray;
視頻截圖三
這是最快捷視頻切圖片的方法,Asset中的最快的方法,使用塊來進行截圖,速度就比其他快上賊多
//pragma mark --- 視頻截取 -(void)cutVideoGetImageArrayWithURl:(NSURL *)urlstring Second:(float)second{ _imageArray = [NSMutableArray array]; AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:urlstring options:nil];// //獲取視頻時長,單位:秒 AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset]; generator.appliesPreferredTrackTransform = YES; generator.maximumSize = _videoRect.size; //下面兩句是截取每一幀的關鍵 generator.requestedTimeToleranceBefore = kCMTimeZero; generator.requestedTimeToleranceAfter = kCMTimeZero; NSMutableArray *timeArray = [NSMutableArray array]; for (int i = 1; i < second * 10 + 1 ; i++) { [timeArray addObject:[NSValue valueWithCMTime:CMTimeMake(i * 6, 60)]]; } //開始截圖 [generator generateCGImagesAsynchronouslyForTimes:timeArray completionHandler:^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) { UIImage *imageSen = [UIImage imageWithCGImage:image]; [_imageArray addObject:imageSen]; }]; }
以上。
iOS 視頻截取