iOS 视频抓图

阅读更多
现在在iOS 软件开发中,视频技术占领非常广的地位,而我们经常在播放视频的过程中,需要执行很多个功能,入视频回放、视频抓图、等
#pragma mark -  执行视频抓图操作方法
/*
抓图方法一
*/
static int i = 0;
-(UIImage*)screenShotView{
    UIGraphicsBeginImageContextWithOptions(imageView1.frame.size, YES, 0);
    [imageView1.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   
    CGImageRef imageRef = viewImage.CGImage;
   
    //设置截图的区域
    CGRect rect =  CGRectMake(imageView1.frame.origin.x, imageView1.frame.origin.y, imageView1.frame.size.width, imageView1.frame.size.height);
   
    CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *image = [[UIImage alloc]initWithCGImage:imageRefRect];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//保存到图片库
    NSData *imageViewData = UIImagePNGRepresentation(image);
   
    //设置保存路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *pictureName = [NSString stringWithFormat:@"showimage_%d.png",i];
    NSString *saveImagePath  =[documentPath stringByAppendingPathComponent:pictureName];
    NSLog(@"%@",saveImagePath);
   
    //开始写入沙盒
    [imageViewData writeToFile:saveImagePath atomically:YES];
    CGImageRelease(imageRefRect);
    i++;
    return image;
}
/*
抓图方法二
*/

-(UIImage*)shotIamge{
    UIGraphicsBeginImageContext(imageView1.frame.size);
    [imageView1.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *showImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
   
    // 存入相册当中
//    UIImageWriteToSavedPhotosAlbum(showImage, nil, nil, nil);
   
    //写入沙盒当中
    NSData *imageData = UIImagePNGRepresentation(showImage);
    //设置路径
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    //获取当前的时间点
    NSDate *date = [NSDate date];
    NSDateFormatter *forMatter = [[NSDateFormatter alloc]init];
    [forMatter setDateFormat:@"YYYY-MM-dd/HH:mm:ss:mm"];
     NSString *nowData=[forMatter stringFromDate:date];
   
    NSString *pictureName = [NSString stringWithFormat:@"%@.png",nowData];
   NSString *saveImagePath  =[documentPath stringByAppendingPathComponent:pictureName];
//    NSLog(@"saveImagePath:%@",saveImagePath);
//    NSLog(@"imageData:%@",imageData);
    //存入沙盒
  BOOL yes =  [imageData writeToFile:saveImagePath atomically:YES];
    if (yes) {
        NSLog(@"%hhd",yes);
        NSLog(@"存入沙盒成功");
    }else{
         NSLog(@"%hhd",yes);
        NSLog(@"存入沙盒失败");
    }
   
    Image *m = [[Image alloc]init];
    m.I_ImageName = pictureName;
   
    // 将图片名字的model类存入数据库
    [[ImageBaseCtl sharedDataBase]AddDataDeviceInfoToDataBase:m];
   
    return showImage;
   
   
   
}

你可能感兴趣的:(iOS,开发篇-视频截图)