您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: