iOS有关截图的操作

时间:2022-08-13 08:15:23

1.截取选中view的图片

 //根据size大小创建一个基于位图的图形上下文
CGRect rect =view.frame; UIGraphicsBeginImageContext(rect.size);//根据size大小创建一个基于位图的图形上下文
CGContextRef context = UIGraphicsGetCurrentContext();//获取上下文环境
[view.layer renderInContext:context];//将要截取的view 映射到画布上 UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); //获得图片
UIGraphicsEndImageContext();
imgvc.image = img;
[view setHidden:YES];

2.两张图片 合成一张图片

 //根据 底层的imageview 大笑 来创建 画布
UIGraphicsBeginImageContext(imgVC_bg.image.size); for (UIImageView *imgvc in array_imageView) {
UIImage *img=imgvc.image;
//画要添加到imgvc上的img
[img drawInRect:CGRectMake(imgvc.frame.origin.x/double_proportion,(imgvc.frame.origin.y-)/double_proportion,imgvc.frame.size.width/double_proportion,imgvc.frame.size.height/double_proportion)];
[imgvc removeFromSuperview];
}
//在画底层imgvc,这样就会把img 添加到imgvc上来。
[imgVC_bg.image drawInRect:CGRectMake(, , imgVC_bg.image.size.width, imgVC_bg.image.size.height)]; //形成新的image
CGImageRef NewMergeImg = CGImageCreateWithImageInRect(UIGraphicsGetImageFromCurrentImageContext().CGImage,
CGRectMake(, , imgVC_bg.image.size.width, imgVC_bg.image.size.height));
UIGraphicsEndImageContext();