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 ...
随机推荐
- vscode创建net core控制台程序
vscode近来深受的开发人员的喜爱.在下的前端同事们也纷纷使用vscode.在下就想了我等后端程序员也可以用vscode写C#代码.毕竟是从宇宙第一IDE ----Visual Studio衍生的 ...
- Linux系统部署samba服务记录
Samba(Server Messages Block)是一种linux系统和windws系统之间依靠网络协议共享文件的服务程序,(Samba has provided secure, stable ...
- java配置环境及安装
1.安装JDK开发环境 首先登陆https://www.oracle.com 第一步:Products ---- Java ---- Download java(JDK) for Developers ...
- [ZJOI2007] 捉迷藏
idea1 可能会死掉的想法:考虑点分治维护每个分治中心x到达分治块内的个点距离,具体是用堆维护分治快内的x的儿子y到y的子树内的所有点距离(记为C[y]),取所有C[y]的top+e(x,y)放入x ...
- chrome 错误 ERR_CACHE_READ_FAILURE
问题现象 谷歌浏览器,点击后退按键提示:ERR_CACHE_READ_FAILURE 错误 解决办法 1. chrome://flags/#enable-simple-cache-backend 2. ...
- LindAgile.SchedulingTask~设计一个不错的任务调度组件
回到目录 SchedulingTask产生的原因 任务调试主要指定期执行某些任务代码,之前用过quartz,感觉有些重,使用时需要添加包包,配置管理项时,对于简单的项目用它就显得有些臃肿了,不如直接上 ...
- LindDotNetCore~授权中间件的介绍
回到目录 LindDotNetCore中间件 大叔认识中间件就是主要对http请求进行拦截,然后添加具体个性化功能的逻辑,这种把请求切开,添加新逻辑的方式一般称为面向方面的逻辑AOP! 授权中间件 请 ...
- MySQL自定义函数与存储过程
1.前置条件 MySQL数据库中存在表user_info,其结构和数据如下: mysql> desc user_info; +-----------+----------+------+---- ...
- .netcore 模块积累
最全的 demo https://github.com/XiaoFaye/netcore-samples http://files.cnblogs.com/files/kellynic/practic ...
- 深入理解Linux内核 学习笔记(1)
1.用户和用户组 每个用户是一个或多个用户组的一名成员,组由唯一的用户组标识符(user group ID)标识.每个文件的相关权限也恰好与一个组相对应. root为超级用户, 2.模块 为了达到微内 ...