博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iPhone/iPad全屏截图与区域截图的几种方法
阅读量:4111 次
发布时间:2019-05-25

本文共 3630 字,大约阅读时间需要 12 分钟。

截取本区域(self.view):

1
2
3
4
5
UIGraphicsBeginImageContext
(CGSizeMake
(self.view.frame.size.width, self.view.frame.size.height
)
);
   
[self.view.layer renderInContext
:UIGraphicsGetCurrentContext
(
)
];
    UIImage
*viewImage
= UIGraphicsGetImageFromCurrentImageContext
(
);
    UIGraphicsEndImageContext
(
);
    UIImageWriteToSavedPhotosAlbum
(viewImage,
nil,
nil,
nil
);

全屏截图:

1
2
3
4
5
6
UIWindow
*screenWindow
=
[
[UIApplication sharedApplication
] keyWindow
];
    UIGraphicsBeginImageContext
(screenWindow.frame.size
);
   
[screenWindow.layer renderInContext
:UIGraphicsGetCurrentContext
(
)
];
    UIImage
*viewImage
= UIGraphicsGetImageFromCurrentImageContext
(
);
    UIGraphicsEndImageContext
(
);
    UIImageWriteToSavedPhotosAlbum
(viewImage,
nil,
nil,
nil
);

以上2种方法真机和模拟器都可以运行.在photo.app里可以看到照片

苹果最新开放的接口函数(全屏截图),已经有人试过了,不会reject:

1
2
3
4
5
CGImageRef UIGetScreenImage
(
);
    CGImageRef img
= UIGetScreenImage
(
);
    UIImage
* scImage
=
[UIImage imageWithCGImage
:img
];
    UIImageWriteToSavedPhotosAlbum
(scImage,
nil,
nil,
nil
);
It still works,but only on
-device
(not
in simulator
) .

截图另存为指定名字:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
UIWindow
*screenWindow
=
[
[UIApplication sharedApplication
] keyWindow
];
UIGraphicsBeginImageContext
(screenWindow.frame.size
);
[screenWindow.layer renderInContext
:UIGraphicsGetCurrentContext
(
)
];
UIImage
*screenshot
= UIGraphicsGetImageFromCurrentImageContext
(
);
UIGraphicsEndImageContext
(
);
*screenshotPNG
= UIImagePNGRepresentation
(screenshot
);
*paths
= NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask,
YES
);
*documentsDirectory
=
[paths objectAtIndex
:
0
];
*error
=
nil;
[screenshotPNG writeToFile
:
[documentsDirectory stringByAppendingPathComponent
:
@
"screenshot.png"
] options
:NSAtomicWrite error
:&error
];

部分代码来自:http://stackoverflow.com/questions/692464/emailing-full-screen-of-iphone-app

没有ipad真机截图发布app的可以用此方法做个透明按钮点,哈哈.

截取本区域(self.view):

1
2
3
4
5
UIGraphicsBeginImageContext
(CGSizeMake
(self.view.frame.size.width, self.view.frame.size.height
)
);
   
[self.view.layer renderInContext
:UIGraphicsGetCurrentContext
(
)
];
    UIImage
*viewImage
= UIGraphicsGetImageFromCurrentImageContext
(
);
    UIGraphicsEndImageContext
(
);
    UIImageWriteToSavedPhotosAlbum
(viewImage,
nil,
nil,
nil
);

全屏截图:

1
2
3
4
5
6
UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screenWindow.frame.size);
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

以上2种方法真机和模拟器都可以运行.在photo.app里可以看到照片

苹果最新开放的接口函数(全屏截图),已经有人试过了,不会reject:

1
2
3
4
5
CGImageRef UIGetScreenImage
(
);
    CGImageRef img
= UIGetScreenImage
(
);
    UIImage
* scImage
=
[UIImage imageWithCGImage
:img
];
    UIImageWriteToSavedPhotosAlbum
(scImage,
nil,
nil,
nil
);
It still works,but only on
-device
(not
in simulator
) .

截图另存为指定名字:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
UIWindow
*screenWindow
=
[
[UIApplication sharedApplication
] keyWindow
];
UIGraphicsBeginImageContext
(screenWindow.frame.size
);
[screenWindow.layer renderInContext
:UIGraphicsGetCurrentContext
(
)
];
UIImage
*screenshot
= UIGraphicsGetImageFromCurrentImageContext
(
);
UIGraphicsEndImageContext
(
);
*screenshotPNG
= UIImagePNGRepresentation
(screenshot
);
*paths
= NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask,
YES
);
*documentsDirectory
=
[paths objectAtIndex
:
0
];
*error
=
nil;
[screenshotPNG writeToFile
:
[documentsDirectory stringByAppendingPathComponent
:
@
"screenshot.png"
] options
:NSAtomicWrite error
:&error
];

转载地址:http://ciosi.baihongyu.com/

你可能感兴趣的文章
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
查找最大值最小值
查看>>
杨辉三角
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
16、Memento 备忘录模式
查看>>
Java基础篇(一)
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python猜拳游戏
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>