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

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

二.实现方式

苹果在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. 每日分享!~ JavaScript(拖拽事件)

    浏览器的拖拉事件 拖拉(drag)指的是,用户在某个对象上按下鼠标键不放,拖动它到另一个位置,然后释放鼠标键,将该对象放在那里. 拖拉的对象有好几种,包括元素节点.图片.链接.选中的文字等等.在网页中 ...

  2. Linux 进程终止后自动重启

    /opt/a.sh #! /bin/bash ps -ef | grep python3 a.py | grep -v grep | grep python3 if [ $? -ne 0 ] then ...

  3. 以写作为例说下IT人如何培养挣钱DNA

    洛克菲勒说:“如果把我剥得一文不名丢在沙漠的中央,只要一行驼队经过——我就可以重建整个王朝.”这话反过来可以这样说,方法不对路,也不肯干的人,哪怕给一笔财富(比如人生小目标一个亿),最好的结果是跑赢C ...

  4. Mybatis之旅第二篇-Mapper动态代理方式

    一.引言 通过上一篇mybatis的入门学习,我们已经会使用mybatis实现简单的增删改查,但是我们也发现了用原始Dao开发的一些问题: Dao方法体存在重复代码:通过SqlSessionFacto ...

  5. Python爬虫入门教程 42-100 爬取儿歌多多APP数据-手机APP爬虫部分

    1. 儿歌多多APP简单分析 今天是手机APP数据爬取的第一篇案例博客,我找到了一个儿歌多多APP,没有加固,没有加壳,没有加密参数,对新手来说,比较友好,咱就拿它练练手,熟悉一下Fiddler和夜神 ...

  6. vue和mpvue

    一.mixins的理解     vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用.最开始我一度认为这个和组件好像没啥区别..后来发现错了.下面我们来看看mixins和普通情况 ...

  7. 一个box-sizing: border-box和felx混合使用中遇到的问题

    之前在项目中遇到一个布局上很趣的问题(也可能是笔者才疏学浅,哈哈).布局大概是这样的: 外层包裹器:采用flex布局,并指定内部子弹性盒子元素水平显示 侧边栏:flex盒子的子元素,可收起和展开.展开 ...

  8. MySQL 笔记整理(13) --为什么数据表删掉一半,表文件大小不变?

    笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 13) --为什么数据表删掉一半,表文件大小不变? 我们还是以MySQL ...

  9. 筛选出和该元素相交的元素之BoundingBoxIntersectsFilter

    //假设元素为ee BoundingBoxXYZ box = ee.get_BoundingBox(doc.ActiveView); //创建outline,通过boundingboxintersec ...

  10. Keepalived 的使用

    1.什么是keepalived Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当w ...