iOS代码模拟Home+power键实现截屏
2016-11-22 12:05
393 查看
导入 #import <AudioToolbox/AudioToolbox.h>
截屏代码如下:
-(void)saveScreen
{
[selfplaySound];
CGSize imageSize = [[UIScreenmainScreen]
bounds].size;
if (NULL != &UIGraphicsBeginImageContextWithOptions) {
UIGraphicsBeginImageContextWithOptions(imageSize,NO,
0);
}
else
{
UIGraphicsBeginImageContext(imageSize);
}
CGContextRef context =UIGraphicsGetCurrentContext();
for (UIWindow * windowin [[UIApplicationsharedApplication]
windows]) {
if (![windowrespondsToSelector:@selector(screen)] || [windowscreen]
== [UIScreenmainScreen]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, [windowcenter].x, [windowcenter].y);
CGContextConcatCTM(context, [windowtransform]);
CGContextTranslateCTM(context, -[windowbounds].size.width*[[windowlayer]
anchorPoint].x, -[windowbounds].size.height*[[windowlayer]
anchorPoint].y);
[[window layer]renderInContext:context];
CGContextRestoreGState(context);
}
}
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image,self,
@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError
*)error contextInfo:(void *)contextInfo
{
if (!error) {
_HUD.labelText =@"保存成功";
_time=1;
}else
{
_HUD.labelText =@"保存失败";
_time=0;
}
[_HUDhide:YESafterDelay:1.0];
}
//模拟播放快门声音加白光
-(void) playSound
{
UIView * view = [[UIView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
view.backgroundColor = [UIColor
whiteColor];
UIWindow * window = [UIApplication
sharedApplication].keyWindow;
[window addSubview:view];
AudioServicesPlaySystemSound(shake_sound_male_id);
[ UIView
animateWithDuration:0.5
animations:^{
view.alpha =
0.05;
} completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
快门声ID如下:
static SystemSoundID shake_sound_male_id =1108;
使用如下方法播放系统声音的ID
AudioServicesPlaySystemSound(shake_sound_male_id);
具体ID连接如下
http://iphonedevwiki.net/index.php/AudioServices
截屏代码如下:
-(void)saveScreen
{
[selfplaySound];
CGSize imageSize = [[UIScreenmainScreen]
bounds].size;
if (NULL != &UIGraphicsBeginImageContextWithOptions) {
UIGraphicsBeginImageContextWithOptions(imageSize,NO,
0);
}
else
{
UIGraphicsBeginImageContext(imageSize);
}
CGContextRef context =UIGraphicsGetCurrentContext();
for (UIWindow * windowin [[UIApplicationsharedApplication]
windows]) {
if (![windowrespondsToSelector:@selector(screen)] || [windowscreen]
== [UIScreenmainScreen]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, [windowcenter].x, [windowcenter].y);
CGContextConcatCTM(context, [windowtransform]);
CGContextTranslateCTM(context, -[windowbounds].size.width*[[windowlayer]
anchorPoint].x, -[windowbounds].size.height*[[windowlayer]
anchorPoint].y);
[[window layer]renderInContext:context];
CGContextRestoreGState(context);
}
}
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image,self,
@selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:),nil);
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError
*)error contextInfo:(void *)contextInfo
{
if (!error) {
_HUD.labelText =@"保存成功";
_time=1;
}else
{
_HUD.labelText =@"保存失败";
_time=0;
}
[_HUDhide:YESafterDelay:1.0];
}
//模拟播放快门声音加白光
-(void) playSound
{
UIView * view = [[UIView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
view.backgroundColor = [UIColor
whiteColor];
UIWindow * window = [UIApplication
sharedApplication].keyWindow;
[window addSubview:view];
AudioServicesPlaySystemSound(shake_sound_male_id);
[ UIView
animateWithDuration:0.5
animations:^{
view.alpha =
0.05;
} completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
快门声ID如下:
static SystemSoundID shake_sound_male_id =1108;
使用如下方法播放系统声音的ID
AudioServicesPlaySystemSound(shake_sound_male_id);
具体ID连接如下
http://iphonedevwiki.net/index.php/AudioServices
相关文章推荐
- iOS 开发中 代码实现 模拟内存警告
- 二十七,iOS的截屏代码实现
- <iOS>iPhone 应用里实现截屏功能的代码
- iOS 截屏代码的实现
- iOS录屏和截屏监听的实现代码
- IOS 开发之ios视频截屏的实现代码
- jQuery的实现原理的模拟代码 -4 重要的扩展函数 extend
- 介绍如何通过代码实现模拟按键的函数
- 介绍如何通过代码实现模拟按键的函数
- jQuery的实现原理的模拟代码 -2 数据部分
- 【flash AS3.0提问】如何用纯as3.0代码 实现对某个图片模拟粒子
- 模拟QQ隐藏效果的实现(示例+代码+隐藏类)
- 全代码实现模拟时钟
- PHP 模拟$_PUT实现代码
- PHP 模拟$_PUT实现代码
- 键盘模拟鼠标(实现用键盘操作鼠标光标)(示例代码下载)
- PostHttpPage用asp是实现模拟登录效果的代码
- Moss2007 提升代码的运行权限,实现模拟管理员身份的功能