经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
iOS 关于监听手机截图,UIView生成UIImage, UIImage裁剪与压缩的总结
来源:cnblogs  作者:甘林梦  时间:2018/12/27 9:36:08  对本文有异议

一.  关于监听手机截图

1. 背景: 发现商品的售价页总是被人转发截图,为了方便用户添加截图分享的小功能

首先要注册用户截屏操作的通知

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. //注册用户的截屏操作通知
  4. [[NSNotificationCenter defaultCenter] addObserver:self
  5. selector:@selector(userDidTakeScreenshot:)
  6. name:UIApplicationUserDidTakeScreenshotNotification object:nil];
  7. }

之后人为截图

  1. // 截屏响应
  2. - (void)userDidTakeScreenshot:(NSNotification *)notification
  3. {
  4. //人为截屏, 模拟用户截屏行为, 获取所截图片
  5. _screenshotImg = [UIutils imageWithScreenshot]; // 这里封装了一下 获得图片就可以取分享啦
  6. DhshareActionSheetScreenshot *shareAlert = [[DhshareActionSheetScreenshot alloc] init];
  7. [shareAlert showWithImg:_screenshotImg];
  8. }

人为截图,在这里可以对图片进行一些操作,比如添加自己的APP二维码啥的类似微博

  1. /**
  2. * 截取当前屏幕 并修改
  3. */
  4. + (UIImage *)imageWithScreenshot
  5. {
  6. CGSize imageSize = CGSizeZero;
  7. UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  8. if (UIInterfaceOrientationIsPortrait(orientation))
  9. imageSize = [UIScreen mainScreen].bounds.size;
  10. else
  11. imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
  12. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
  13. CGContextRef context = UIGraphicsGetCurrentContext();
  14. for (UIWindow *window in [[UIApplication sharedApplication] windows])
  15. {
  16. CGContextSaveGState(context);
  17. CGContextTranslateCTM(context, window.center.x, window.center.y);
  18. CGContextConcatCTM(context, window.transform);
  19. CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
  20. if (orientation == UIInterfaceOrientationLandscapeLeft)
  21. {
  22. CGContextRotateCTM(context, M_PI_2);
  23. CGContextTranslateCTM(context, 0, -imageSize.width);
  24. }else if (orientation == UIInterfaceOrientationLandscapeRight)
  25. {
  26. CGContextRotateCTM(context, -M_PI_2);
  27. CGContextTranslateCTM(context, -imageSize.height, 0);
  28. } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
  29. CGContextRotateCTM(context, M_PI);
  30. CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
  31. }
  32. if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
  33. {
  34. [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
  35. }
  36. else
  37. {
  38. [window.layer renderInContext:context];
  39. }
  40. CGContextRestoreGState(context);
  41. }
  42. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  43. UIGraphicsEndImageContext();
  44. return image;
  45. }

当然最后不要忘记注销通知,这里要注意,根据需求来,如果商品详情页又可以跳转到别的商品详情页最好这样注销,

避免跳到别的商品详情页多次截图

  1. - (void)viewWillDisappear:(BOOL)animated {
  2. [super viewWillDisappear:animated];
  3. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
  4. }

二. UIView生成UIImag

UIView生成UIImage 时可以转一下jpg格式,这样图片不会太大具体参数可以百度,屏幕密度我一般采用0.7;

  1. // UIView生成图片
  2. +(UIImage*)convertViewToImage:(UIView*)v{
  3. CGSize s = v.bounds.size;
  4. // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
  5. // NSLog(@"[UIScreen mainScreen].scale-%f",[UIScreen mainScreen].scale);
  6. // 3.0 高清图 分享不了 用2.0即可 或者分享的时候压缩图片
  7. UIGraphicsBeginImageContextWithOptions(s, YES, [UIScreen mainScreen].scale);
  8. [v.layer renderInContext:UIGraphicsGetCurrentContext()];
  9. UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
  10. UIGraphicsEndImageContext();
  11. NSData * data = UIImageJPEGRepresentation(image, 0.7);
  12. UIImage* imagelast = [UIImage imageWithData:data];
  13. return imagelast;
  14. }

三. UIImage的裁剪

  1. // 图片裁剪
  2. + (UIImage *)ct_imageFromImage:(UIImage *)image inRect:(CGRect)rect{
  3. //把像 素rect 转化为 点rect(如无转化则按原图像素取部分图片)
  4. CGFloat scale = [UIScreen mainScreen].scale;
  5. CGFloat x= rect.origin.x*scale,y=rect.origin.y*scale,w=rect.size.width*scale,h=rect.size.height*scale;
  6. CGRect dianRect = CGRectMake(x, y, w, h);
  7. //截取部分图片并生成新图片
  8. CGImageRef sourceImageRef = [image CGImage];
  9. CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, dianRect);
  10. UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
  11. NSData * data = UIImageJPEGRepresentation(newImage, 0.7);
  12. UIImage* imagelast = [UIImage imageWithData:data];
  13. return imagelast;
  14. }

四. UIImage的压缩

分享图片时根据分享的应用不同对图片大小是有限制的,微信是10M,但是qq小一些,

另外小程序分享的话也是有限制的128Kb,当截图裁剪后无法满足时就需要压缩一下

先是密度压缩然后大小压缩,一般都可以解决的

  1. // 图片压缩
  2. + (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
  3. // Compress by quality
  4. CGFloat compression = 0.7;
  5. //UIImage转换为NSData
  6. NSData *data = UIImageJPEGRepresentation(image, compression);
  7. if (data.length < maxLength){
  8. return image;
  9. }
  10. CGFloat max = 1;
  11. CGFloat min = 0;
  12. for (int i = 0; i < 6; ++i) {
  13. compression = (max + min) / 2;
  14. data = UIImageJPEGRepresentation(image, compression);
  15. if (data.length < maxLength * 0.9) {
  16. min = compression;
  17. } else if (data.length > maxLength) {
  18. max = compression;
  19. } else {
  20. break;
  21. }
  22. }
  23. UIImage *resultImage = [UIImage imageWithData:data];
  24. if (data.length < maxLength){
  25. return resultImage;
  26. }
  27. // Compress by size
  28. NSUInteger lastDataLength = 0;
  29. while (data.length > maxLength && data.length != lastDataLength) {
  30. lastDataLength = data.length;
  31. CGFloat ratio = (CGFloat)maxLength / data.length;
  32. CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
  33. (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
  34. UIGraphicsBeginImageContext(size);
  35. [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
  36. resultImage = UIGraphicsGetImageFromCurrentImageContext();
  37. UIGraphicsEndImageContext();
  38. data = UIImageJPEGRepresentation(resultImage, compression);
  39. }
  40. return resultImage;
  41. }

这些是最近APP的截图分享,小程序分享的关于图片的总结.希望帮助到需要的人.

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号