iOS开发 截图分享之截图及修改图片大小和分辨率(截满屏,截长图
2022-04-18 本文已影响0人
阳光下的叶子呵
无论是从用户体验角度还是产品运营角度截图分享功能已经覆盖大部分的APP。本文不介绍如何分享,只介绍几种截屏的方法(原理相同)!希望能帮助有需要的朋友。
不同的产品对功能的需求有所不同,有些APP只要求截取一屏的内容,有些APP需要截取超出一屏的内容。
方法1、截取屏幕上指定view的内容:
/** 1、截取屏幕上指定view的内容 */
- (UIImage *)shotShareImageFromView:(UIView *)view withHeight:(CGFloat)height {
//高清方法
//第一个参数表示区域大小 第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
CGSize size = CGSizeMake(view.layer.bounds.size.width, height);
UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
方法2、截取UIScrollView上所有内容:
/** 2、截取UIScrollView上所有内容 */
- (UIImage *)shotShareImageFromView:(UIView *)view withHeight:(CGFloat)height {
//高清方法
//第一个参数表示区域大小 第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
CGSize size = CGSizeMake(view.layer.bounds.size.width, height);
UIGraphicsBeginImageContextWithOptions(size, YES, [UIScreen mainScreen].scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
/** 2、截取UIScrollView上所有内容 */
- (UIImage *)captureScrollView:(UIScrollView *)scrollView {
UIImage *image = nil;
//第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,设置为[UIScreen mainScreen].scale可以保证转成的图片不失真。
// UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, YES, 0.0);
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, YES, [UIScreen mainScreen].scale);
// 正确写法:跳转页面的时候(比如分享截图的时候,跳转再返回时,不会发生uiscrollview的frame向上移的问题。)
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.frame = CGRectMake(0 , 0, scrollView.contentSize.width, scrollView.contentSize.height);
[scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
UIGraphicsEndImageContext();
// 下面这样写,会发生向上平移问题:
// //这里需要你修改下layer的frame,不然只会截出在屏幕显示的部分
// scrollView.layer.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
// [scrollView.layer renderInContext:UIGraphicsGetCurrentContext()];
// image = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
if (image != nil) {
return image;
}
return nil;
}
方法3、把两张图片合成为一张图片(这里是上下拼接的):
/** 把两张图片合成为一张图片 */
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
// UIGraphicsBeginImageContext(image1.size);
// size:宽度一样,高相加(两张图片 上下拼接成一张图片)
// UIGraphicsBeginImageContext(CGSizeMake(image1.size.width, image1.size.height + image2.size.height)); // 截图模糊
UIGraphicsBeginImageContextWithOptions(CGSizeMake(image1.size.width, image1.size.height + image2.size.height), YES, [UIScreen mainScreen].scale); // 保持截图清晰
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
// Draw image2(注意:这里需要你修改下Y轴距离,两张图片 上下拼接成一张图片)
[image2 drawInRect:CGRectMake(0, image1.size.height, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
方法4、对图片进行压缩和裁剪:
- (NSData *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImageJPEGRepresentation(newImage, 0.8);
}
5、调整图片分辨率/尺寸(等比例缩放):
- (UIImage *)newSizeImage:(CGSize)size image:(UIImage *)sourceImage {
CGSize newSize = CGSizeMake(sourceImage.size.width, sourceImage.size.height);
CGFloat tempHeight = newSize.height / size.height;
CGFloat tempWidth = newSize.width / size.width;
if (tempWidth > 1.0 && tempWidth > tempHeight) {
newSize = CGSizeMake(sourceImage.size.width / tempWidth, sourceImage.size.height / tempWidth);
} else if (tempHeight > 1.0 && tempWidth < tempHeight) {
newSize = CGSizeMake(sourceImage.size.width / tempHeight, sourceImage.size.height / tempHeight);
}
UIGraphicsBeginImageContext(newSize);
[sourceImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
方法6、截取image指定区域的图片:
- ( UIImage *)getImageByCuttingImage:( UIImage *)image ToRect:( CGRect )rect {
// 大图 bigImage
// 定义 myImageRect ,截图的区域
CGRect toImageRect = rect;
UIImage *bigImage= image;
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, toImageRect);
CGSize size;
size. width = rect.size.width;
size. height = rect.size.height;
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextDrawImage(context, toImageRect, subImageRef);
UIImage *smallImage = [UIImage imageWithCGImage:subImageRef];
UIGraphicsEndImageContext();
return smallImage;
}