iOS截屏并修改截图然后分享的功能实现
一. 实现的效果类似微博的截图分享


不仅截图分享的时候还进行图片的修改,增加自己的二维码
二.实现方式
苹果在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];// 微信朋友圈
// [MyAPIClient mobEvent:@"QQ"];
// [Helper shareImageName:_testImg type:SSDKPlatformTypeQQ];// QQ }
5. 移除通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
这样就可以了.展示一下测试
自身截图

截图修改分享图

ok,结束,需要补充的,欢迎大家留言讨论!
iOS截屏并修改截图然后分享的功能实现的更多相关文章
- iOS - 截屏,view截图的基本方法
推荐一个第三方好用的框架:SDScreenshotCapture #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice cur ...
- iOS 截屏分享(包含状态栏与不包含状态栏)
iOS8以上的新方法PhotoKit 监听截图相册变化,取最后一张图片:http://www.hangge.com/blog/cache/detail_1515.html PhotoKit 获取本机相 ...
- Android 截屏与 WebView 长图分享经验总结
最近在做新业务需求的同时,我们在 Android 上遇到了一些之前没有碰到过的问题,截屏分享. WebView 生成长图以及长图在各个分享渠道分享时图片模糊甚至分享失败等问题,在这过程中踩了很多坑,到 ...
- ios截屏代码[转]
http://www.cnblogs.com/chenxiangxi/p/3547974.html 这位博主的连接中将ios自定义大小位置的截屏代码写的很不错,马上就能用的方法,对于只想马上用的程序员 ...
- iOS截屏代码
转载自:http://m.open-open.com/m/code/view/1420469506375 1.普通界面 /** *截图功能 */ -(void)screenShot{ UIGraphi ...
- iOS截屏保存至相册
#pragma mark 截屏并保存至相册 -(void)screenShotsComplete:(void(^)(UIImage * img)) complete { CGSize imageSiz ...
- iOS截屏方法
//获取屏幕截屏方法 - (UIImage *)capture { // 创建一个context UIGraphicsBeginImageContextWithOptions(self.view.bo ...
- iOS截屏功能
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...
- IOS 截屏(保存到相册中)
@interface NJViewController () /** * 点击截屏按钮 */ - (IBAction)captureView:(UIButton *)sender; /** * 白色v ...
随机推荐
- Python爬虫9-request包介绍及应用
GitHub代码练习地址:1.两种简单get请求方法:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac13_request ...
- Java相关面试题总结+答案(三)
[多线程] 35. 并行和并发有什么区别? 并行:多个处理器或多核处理器同时处理多个任务.(是真正的物理上的同时发生) 并发:多个任务在同一个 CPU 核上,按细分的时间片轮流(交替)执行,从逻辑上来 ...
- jquery mobile 建wap站
使用jquery mobile 建手机wap站: 几篇比较好的文章 http://wap.yesky.com/dev/225/30974725.shtml http://tech.it168.com/ ...
- load data infile
mysql> load data local infile 'd:/lw.txt' into table user (code,password,channelId);Query OK, 478 ...
- 写给 Android 开发的小程序布局指南,Flex 布局!
一.序 Hi,大家好,我是承香墨影! 最近在做小程序,验证一些方向,开发效率确实很快,就是各种微信的审核有点费劲,但是总归是有办法解决的. 想要开发一款小程序,其实和我们正常写一款 App 类似,你需 ...
- ShareDialogDemo【分享对话框】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 整理项目中经常用到的分享对话框的实现. 这里只是一个分享对话框,不涉及分享功能的实现. 效果图 代码分析 ShareDialog.j ...
- 《HelloGitHub》第 28 期
<HelloGitHub>第 28 期 兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程. ...
- DotNetCore跨平台~EFCore2.0连接Mysql的烦恼-已解决
回到目录 对于传统的nuget包,我们习惯上用官方或者大型组织的,因为它们比较考靠谱,但就在前两天.net core2.0发布后,我把efcore也升级到2.0了,mysql.EfCore也升级到支持 ...
- LindDotNetCore~Scheduling任务调度模块的介绍
回到目录 任务调度组件 位于Scheduling目录 基类JobBase,所有JOB都派生自它,重写Cron属性可以修改调度周期 支持单次JOB,即执行完成后马上停止 支持对外API接口,以便获取和修 ...
- Redis学习笔记~Twenproxy所起到的作用
回到目录 Twenproxy除了可以作为redis的代理,它同样支持memerycached.我这里主要了解Twemproxy在redis集群上的解决方案.Twemproxy除了完美的解决了分片,路由 ...