一. 实现的效果类似微博的截图分享

不仅截图分享的时候还进行图片的修改,增加自己的二维码

二.实现方式

苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScreenshotNotification,

这个通知会告知注册了此通知的对象已经发生了截屏事件,然后我们就可以在这个事件中实现自己的逻辑

1.注册通知

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

2.接收通知 (获取截图并修改的图片,并展示,展示UI,可以自己修改)

//截屏响应
- (void)userDidTakeScreenshot:(NSNotification *)notification
{
NSLog(@"检测到截屏"); //人为截屏, 模拟用户截屏行为, 获取所截图片
_testImg = [self imageWithScreenshot]; // //添加显示
UIImageView *imgvPhoto = [[UIImageView alloc]initWithImage:_testImg];
imgvPhoto.frame = CGRectMake(, WIN_HEIGHT/, WIN_WIDTH/, WIN_HEIGHT/);
imgvPhoto.backgroundColor = [UIColor orangeColor];
imgvPhoto.userInteractionEnabled = YES;
//添加边框
CALayer * layer = [imgvPhoto layer];
layer.borderColor = [[UIColor whiteColor] CGColor];
layer.borderWidth = 5.0f;
//添加四个边阴影
imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
imgvPhoto.layer.shadowOffset = CGSizeMake(, );
imgvPhoto.layer.shadowOpacity = 0.5;
imgvPhoto.layer.shadowRadius = 10.0;
//添加两个边阴影
imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;
imgvPhoto.layer.shadowOffset = CGSizeMake(, );
imgvPhoto.layer.shadowOpacity = 0.5;
imgvPhoto.layer.shadowRadius = 2.0; [self.view addSubview:imgvPhoto]; // 添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImgView:)];
[imgvPhoto addGestureRecognizer:tap];
}

3. 截图并修改图片

/**
* 截取当前屏幕 并修改
*
* @return NSData *
*/
- (UIImage *)imageWithScreenshot
{
CGSize imageSize = CGSizeZero;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsPortrait(orientation))
imageSize = [UIScreen mainScreen].bounds.size;
else
imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width); UIGraphicsBeginImageContextWithOptions(imageSize, NO, );
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
CGContextSaveGState(context);
CGContextTranslateCTM(context, window.center.x, window.center.y);
CGContextConcatCTM(context, window.transform);
CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
if (orientation == UIInterfaceOrientationLandscapeLeft)
{
CGContextRotateCTM(context, M_PI_2);
CGContextTranslateCTM(context, , -imageSize.width);
}else if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, );
} else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGContextRotateCTM(context, M_PI);
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
}
if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
{
[window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
}
else
{
[window.layer renderInContext:context];
}
CGContextRestoreGState(context);
} UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); // 修改图片
NSData *imageData = UIImagePNGRepresentation(image);
UIImage *LastImage = [UIImage imageWithData:imageData]; UIImage *img = [UIImage imageNamed:@"ico_nursery.png"];
CGImageRef imgRef = img.CGImage;
CGFloat w = CGImageGetWidth(imgRef);
CGFloat h = CGImageGetHeight(imgRef); //以1.png的图大小为底图
UIImage *img1 = LastImage;
CGImageRef imgRef1 = img1.CGImage;
CGFloat w1 = CGImageGetWidth(imgRef1);
CGFloat h1 = CGImageGetHeight(imgRef1); //以1.png的图大小为画布创建上下文
UIGraphicsBeginImageContext(CGSizeMake(w1, h1 + ));
[img1 drawInRect:CGRectMake(, , w1, h1)];//先把1.png 画到上下文中
[img drawInRect:CGRectMake(, h1 + , , )];//再把小图放在上下文中
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片
UIGraphicsEndImageContext();//关闭上下文 return resultImg;
}

4.根据添加的事件进行分享 分享自己也可封装

// 点击图片改变imageView位置,打印图片信息  分享自己也可封装
- (void)tapImgView: (UITapGestureRecognizer *)tap { NSLog(@"点击了图片...");
// 微信
[MyAPIClient mobEvent:@"wechat"];
// [Helper shareImageName:_testImg type:SSDKPlatformSubTypeWechatSession];// 微信好友
[Helper shareImageName:_testImg type:SSDKPlatformSubTypeWechatTimeline];// 微信朋友圈
// QQ
// [MyAPIClient mobEvent:@"QQ"];
// [Helper shareImageName:_testImg type:SSDKPlatformTypeQQ];// QQ }

5. 移除通知

- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}

这样就可以了.展示一下测试

自身截图

截图修改分享图

ok,结束,需要补充的,欢迎大家留言讨论!

iOS截屏并修改截图然后分享的功能实现的更多相关文章

  1. iOS - 截屏,view截图的基本方法

    推荐一个第三方好用的框架:SDScreenshotCapture #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice cur ...

  2. iOS 截屏分享(包含状态栏与不包含状态栏)

    iOS8以上的新方法PhotoKit 监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html PhotoKit 获取本机相 ...

  3. Android 截屏与 WebView 长图分享经验总结

    最近在做新业务需求的同时,我们在 Android 上遇到了一些之前没有碰到过的问题,截屏分享. WebView 生成长图以及长图在各个分享渠道分享时图片模糊甚至分享失败等问题,在这过程中踩了很多坑,到 ...

  4. ios截屏代码[转]

    http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员 ...

  5. iOS截屏代码

    转载自:http://m.open-open.com/m/code/view/1420469506375 1.普通界面 /** *截图功能 */ -(void)screenShot{ UIGraphi ...

  6. iOS截屏保存至相册

    #pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSiz ...

  7. iOS截屏方法

    //获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...

  8. iOS截屏功能

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  9. IOS 截屏(保存到相册中)

    @interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...

随机推荐

  1. (转)学习MySQL优化原理,这一篇就够了!

    原文:https://mp.weixin.qq.com/s__biz=MzI4NTA1MDEwNg==&mid=2650763421&idx=1&sn=2515421f09c1 ...

  2. Solr 14 - SolrJ操作SolrCloud集群 (Solr的Java API)

    目录 1 pom.xml文件的配置 2 SolrJ操作SolrCloud 1 pom.xml文件的配置 项目的pom.xml依赖信息请参照: Solr 09 - SolrJ操作Solr单机服务 (So ...

  3. Jenkins|简单Job配置|启动脚本|测试报告

    目录 1.Jenkins安装 2.Jenkins启动脚本 3.节点配置 4.任务配置 5.集成HTML测试报告 1.Jenkins安装 操作环境:Ubuntu jenkins针对windows,ubu ...

  4. Chapter 5 Blood Type——32

    "Where are you all going, exactly?" He was still looking ahead, expressionless. “真的,你要去哪里? ...

  5. 事件Event一

    事件(Event)例如:最近的视觉中国'黑洞事件'.我们大多数人(订阅者)是通过XX平台(发布者)得知的这一消息,然后订阅者A出售视觉中国的股票(触发的方法),订阅者B买入视觉中国的股票. using ...

  6. MySQL 笔记整理(19) --为什么我只查一行的语句,也执行这么慢?

    笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 19) --为什么我只查一行的语句,也执行这么慢? 需要说明一下,如果M ...

  7. java_IO流

    IO流概述及分类 Reader  InputStream  OutputStream  Writer都是Object的直接子类 字节流: 字节输入流 InputStream(抽象类) |---File ...

  8. linux的一些命令,面试经常碰到的

    1 查询所有java进程ps -A | grep java解释一下这个命令的意思,ps 是查看系统当前进程的命令,参数 -A(注意,是大写,linux中是区分大小写的)是显示所有程序,| 这个是管道符 ...

  9. 第二章 Linux目录学习

    Linux 目录结构相对windows来说更简单,Linux 目录 以 斜杠 / 为根目录,其整体结构是以/为根的树状结构. 使用 tree -L 1 查看1级目录结构 /bin 常用的二进制命令目录 ...

  10. SVN的使用说明

    SVN管理版本是比较好的东西,但是我的SVN图标老是不出来,有时候莫名其妙就没有了,所以记录一下处理办法: 1.右键->TortoiseSVN->Settings->Icon Ove ...