iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)
iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)
2017.03.16 12:18* 字数 52 阅读 563评论 4喜欢 2
1. 截取屏幕尺寸大小的图片并保存至相册
保存至相册只需将方法saveImage中的代码替换即可
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
2. scrollView或tableView的全部截屏并保存至相册
- (void)saveImage {
UIImage* viewImage = nil;
UITableView *scrollView = self.tableView;
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, 0.0);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
[self saveImageToPhotos:viewImage];
}
- (void)saveImageToPhotos:(UIImage*)savedImage {
UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"保存图片成功" ;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
message:msg
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
+ (UIImage *)screenshot
{
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, 0);
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, 0, -imageSize.width);
} else if (orientation == UIInterfaceOrientationLandscapeRight) {
CGContextRotateCTM(context, -M_PI_2);
CGContextTranslateCTM(context, -imageSize.height, 0);
} 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();
return image;
}
swift
class func screenshot() -> UIImage {
var imageSize = CGSizeZero
let orientation = UIApplication.sharedApplication().statusBarOrientation
if UIInterfaceOrientationIsPortrait(orientation) {
imageSize = UIScreen.mainScreen().bounds.size
} else {
imageSize = CGSize(width: UIScreen.mainScreen().bounds.size.height, height: UIScreen.mainScreen().bounds.size.width)
}
UIGraphicsBeginImageContextWithOptions(imageSize, false, 0)
let context = UIGraphicsGetCurrentContext()
for 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 == .LandscapeLeft {
CGContextRotateCTM(context, CGFloat(M_PI_2))
CGContextTranslateCTM(context, 0, -imageSize.width)
} else if orientation == .LandscapeRight {
CGContextRotateCTM(context, -CGFloat(M_PI_2))
CGContextTranslateCTM(context, -imageSize.height, 0)
} else if orientation == .PortraitUpsideDown {
CGContextRotateCTM(context, CGFloat(M_PI))
CGContextTranslateCTM(context, -imageSize.width, -imageSize.height)
}
if window.respondsToSelector("drawViewHierarchyInRect:afterScreenUpdates:") {
window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: true)
} else if let context = context {
window.layer.renderInContext(context)
}
CGContextRestoreGState(context)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
链接:https://www.jianshu.com/p/343387a9e5c0
iOS应用内截图代码
有待改写工具类?????
2016.06.17
第一种方法(截全屏幕)
下面这段代码可以将当前屏幕显示的内容截图放置相册中,需要导入
-(void)viewDidAppear:(BOOL)animated
{
[superviewDidAppear:animated];
self.view.backgroundColor= [UIColorgreenColor];
UIWindow*screenWindow = [[UIApplicationsharedApplication]keyWindow];
UIGraphicsBeginImageContext(screenWindow.frame.size);
[screenWindow.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage* viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage,nil,nil,nil);
}
第二种方法(自定义截图区域)
#pragmamark -=====自定义截屏位置大小的逻辑代码=====-
staticintScreenshotIndex=0; //
-(void)ScreenShot{
//这里因为我需要全屏接图所以直接改了,宏定义iPadWithd为1024,iPadHeight为768,
//UIGraphicsBeginImageContextWithOptions(CGSizeMake(640, 960), YES, 0);//设置截屏大小
UIGraphicsBeginImageContextWithOptions(CGSizeMake(iPadWidth, iPadHeight), YES,0);//设置截屏大小
[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef imageRef =viewImage.CGImage;
//CGRect rect = CGRectMake(166, 211, 426, 320);//这里可以设置想要截图的区域
CGRect rect = CGRectMake(0,0, iPadWidth, iPadHeight);//这里可以设置想要截图的区域
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage =[[UIImage alloc] initWithCGImage:imageRefRect];
UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库
NSData *imageViewData =UIImagePNGRepresentation(sendImage);
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pictureName= [NSString stringWithFormat:@"screenShow_%d.png",ScreenshotIndex];
NSString *savedImagePath =[documentsDirectory stringByAppendingPathComponent:pictureName];
NSLog(@"截屏路径打印: %@", savedImagePath);
//这里我将路径设置为一个全局String,这里做的不好,我自己是为了用而已,希望大家别这么写
[self SetPickPath:savedImagePath];
[imageViewData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录
CGImageRelease(imageRefRect);
ScreenshotIndex++;
}
//设置路径
- (void)SetPickPath:(NSString *)PickImage {
_ScreenshotsPickPath =PickImage;
}
//获取路径<这里我就直接用于邮件推送的代码中去了,能达到效果,但肯定有更好的写法>
- (NSString *)GetPickPath {
return_ScreenshotsPickPath;
}
IOS获取设备屏幕代码(截屏)
-(void) screenShot{
UIGraphicsBeginImageContext(self.bounds.size); //self为需要截屏的UI控件 即通过改变此参数可以截取特定的UI控件
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSLog(@"image:%@",image); //至此已拿到image
UIImageView *imaView = [[UIImageView alloc] initWithImage:image];
imaView.frame = CGRectMake(0, 700, 500, 500);
[self addSubview:imaView];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);//把图片保存在本地
}
iOS中的截屏(屏幕截屏及scrollView或tableView的全部截屏)的更多相关文章
- iOS中正确的截屏姿势
昨天写了个用到截屏功能的插件,结果问题不断,今天终于解决好了,把debug过程中所有尝试过的截屏方法都贴出来吧- 第一种 这是iOS 3时代开始就被使用的方法,它被废止于iOS 7.iOS的私有方法, ...
- ios中iframe页面出现白屏问题
最近用ionic3开发的一个项目在ios中出现了白屏的问题 banner轮播图跳转网页 使用了iframe 但是却时不时的出现白屏现象 在android中一切正常 网上查资料发现 是因为ios不允许访 ...
- iOS 中各种横竖屏切换总结
iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...
- 谈谈iOS中的屏幕方向
众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...
- iOS中为网站添加图标到主屏幕以及增加启动画面
虽然没有能力开发Native App,但还是可以利用iOS中Safari浏览器的特性小小的折腾一下,做一个伪Web App满足下小小的虚荣心的. 既然是在iOS中的Safari折腾的,那么代码中利用到 ...
- vmware ubuntu16 启动蓝屏屏幕闪
vmware ubuntu16 启动蓝屏屏幕闪 虚拟机安装了ubuntu16 desktop,没有关闭自动更新: 结果关机虚拟机时出现等5秒更新,类似win10关机更新: 再开机发现就蓝屏了,多次重启 ...
- iOS中为网站添加图标到主屏幕
1 <link rel="apple-touch-icon-precomposed" href="icon.png"/> 2 <link re ...
- iOS中-Qutarz2D详解及使用
在iOS中Qutarz2D 详解及使用 (一)初识 介绍 Quartz 2D是二维绘图引擎. 能完成的工作有: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...
- ios 中的小技巧 - 总有你想要的 一
UITableView的Group样式下顶部空白处理 在viewWillAppear里面添加如下代码: //分组列表头部空白处理 CGRect frame = myTableView.tableHea ...
随机推荐
- SQL Server 锁实验(INSERT加锁探究)
insert语句: 其上锁情况为: insert语句会对表上的所有索引作出更新,因此这里看到的索引列较多,我们先把所有的索引搞出来看看: 可以看到所有索引都涉及到了,然后我们来仔细分析下加锁情况: 1 ...
- php学习----什么是常量
PHP-什么是常量 1.什么是常量?常量可以理解为值不变的量(如圆周率):或者是常量值被定义后,在脚本的其他任何地方都不可以被改变.PHP中的常量分为自定义常量和系统常量(后续小节会详细介绍). 2. ...
- SAP LOGON 快捷登陆方式如何保存密码
默认情况下,快捷方式密码是不能输入的. 解决方法:修改注册表: 计算机\HKEY_CURRENT_USER\Software\SAP\SAPShortcut\Security EnablePasswo ...
- 如何写 go 代码 (How to Write Go Code 翻译)
目录 1. 写在前面的话 2. 介绍 3. 代码组织 3.1. 工作区 3.2. GOPATH 环境变量 3.3. Package 路径 3.4. 第一个 GO 程序 3.5. 第一个 GO 库 3. ...
- main主函数
public static void main(String[] args) { //虚拟机调用main函数,需要传个args的参数,传入的是new String[0] System.out.prin ...
- display:table-cell几种应用
http://www.zhangxinxu.com/wordpress/2010/10/%E6%88%91%E6%89%80%E7%9F%A5%E9%81%93%E7%9A%84%E5%87%A0%E ...
- 如何学习sss和前端数据处理
1.学习scss,就看这篇:http://www.ruanyifeng.com/blog/2012/06/sass.html 就够了,因为sass的出现本来就是为了简化工作提高效率,也不算什么深奥精妙 ...
- 【Java8】@FunctionalInterface
阅读目录 什么是函数式接口(Functional Interface) 函数式接口用途 关于@FunctionalInterface注解 函数式接口里允许定义默认方法 函数式接口里允许定义静态方法 函 ...
- BZOJ2521:[SHOI2010]最小生成树(最小割)
Description Secsa最近对最小生成树问题特别感兴趣.他已经知道如果要去求出一个n个点.m条边的无向图的最小生成树有一个Krustal算法和另一个Prim的算法.另外,他还知道,某一个图可 ...
- oldboy-作业01.登录多次进行账号锁定
"""可以支持多个用户登录 (提示,通过列表存多个账户信息)用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里) &q ...