图片截取和视频截取图片

最近开发一个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];
    }];
    }

以上。

你可能感兴趣的:(图片截取和视频截取图片)