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;



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值