iOS摇一摇截屏功能的实现

1.摇一摇功能的实现:

在viewDidLoad方法中调用:

[[UIApplication sharedApplication]setApplicationSupportsShakeToEdit:YES];

在viewWillAppear方法中调用:

[self becomeFirstResponder];

为了防止意外发生,需要在viewWillDisappear方法中调用:

[self resignFirstResponder];

实现和摇一摇相关的三个方法:

#pragma mark - ShakeToEdit
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //手机震动
    dispatch_queue_t queue = dispatch_queue_create("shake", NULL);
    dispatch_async(queue, ^{
        [self shake];
    });
      
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"cancel");
    
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    UIImage *image = [self  screenshot];
    
    if(image){
        [ShowView showWithImage:image AndSaveClick:^{
            [self saveImageToPhotos:image];
        }];
    }
}

2.屏幕截图功能的实现:

#pragma mark - ScreenShot
-(UIImage *)screenshot{
    
    UIView *view = self.view;
    //  float scale = [[UIScreenmainScreen] scale];//得到设备的分辨率
    //  scale = 1; 的时候是代表当前设备是320*480的分辨率(就是iphone4之前的设备)
    //  scale = 2; 的时候是代表分辨率为640*960的分辨率
    
    //绘图
    //第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数。
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [UIScreen mainScreen].scale);
    //渲染
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    //生产图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    return image;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容