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

本文共 3694 字,大约阅读时间需要 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
(
);
NSData 
*screenshotPNG 
= UIImagePNGRepresentation
(screenshot
);
NSArray 
*paths 
= NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, 
YES
);
NSString 
*documentsDirectory 
= 
[paths objectAtIndex
:
0
];
NSError 
*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
(
);
NSData 
*screenshotPNG 
= UIImagePNGRepresentation
(screenshot
);
NSArray 
*paths 
= NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, 
YES
);
NSString 
*documentsDirectory 
= 
[paths objectAtIndex
:
0
];
NSError 
*error 
= 
nil;
[screenshotPNG writeToFile
:
[documentsDirectory stringByAppendingPathComponent
:
@
"screenshot.png"
] options
:NSAtomicWrite error
:&error
];

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

你可能感兴趣的文章
JavaSE_day_03 方法
查看>>
day-03JavaSE_循环
查看>>
Mysql初始化的命令
查看>>
day_21_0817_Mysql
查看>>
day-22 mysql_SQL 结构化查询语言
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
HTML&CSS进阶
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
MySQL-分布式架构-MyCAT
查看>>
设计模式六大原则(6):开闭原则
查看>>
阿里面试总结--JAVA
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>