截屏分享
////7. 合并两张图片
+(UIImage*)appendingImage:(UIImage*)image1 toImage:(UIImage*)image2 {
CGSizesize1 = image1.size;
CGSizesize =CGSizeMake(MAX(size1.width, image2.size.width), size1.height+image2.size.height);
//然后用UIGraphics进行绘制拼接(在此之前必须将两个图片得宽度变为一致)
UIGraphicsBeginImageContext(size);
// Draw image1
[image1drawInRect:CGRectMake(0,0, size.width, size1.height)];
// Draw image2
[image2drawInRect:CGRectMake(0, size1.height, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnresultingImage;
}
////8.截屏
+(UIImage*)ScreenShot:(UIView*)view rect:(CGRect)rect{
//1.获取需要截取的view,计算截取的范围,缩放比例
CGFloatscale = [UIScreenmainScreen].scale;
CGSize size = CGSizeMake(rect.size.width, rect.size.height);
UIGraphicsBeginImageContextWithOptions(size, NO,scale);//设置截屏大小
//用layer取出图片内容
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
//再用UIGraphics绘制内容image
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
然后再用CGImageRef重绘图片(如果不重绘,你得到得缩放图片会有空白)
CGImageRefimageRef =viewImage.CGImage;
CGImageRefimageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage*sendImage =[[UIImagealloc]initWithCGImage:imageRefRect];
CGImageRelease(imageRefRect);
returnsendImage;
}