zoukankan      html  css  js  c++  java
  • ios摇一摇截屏代码

    #import "ViewController.h"

     

    @interface ViewController ()

     

    @end

     

    @implementation ViewController

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        //1.添加一个视图

        UIView *greenView=[[UIView alloc]init];

        greenView.frame=CGRectMake(100, 100, 100, 100);

        greenView.backgroundColor=[UIColor greenColor];

        [self.view addSubview:greenView];

        //设置第一响应事件(必须做的)

        [self becomeFirstResponder];

        

        

    }

    -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event

    {

        if (motion==UIEventSubtypeMotionShake) {

            //截屏

            [self snapshot];

        }

    }

     

    //截屏

    -(void)snapshot

    {

        //开启上下文

        UIGraphicsBeginImageContext(self.view.bounds.size);

        //拿到上下文

        CGContextRef context=UIGraphicsGetCurrentContext();

        [self.view.layer renderInContext:context];

        UIImage *image=UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        //保存到相册

        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    }

    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo

    {

        if (!error) {

            NSLog(@"save success");

        }

        

    }

     

    @end

  • 相关阅读:
    github fork项目后,代码更新
    UIScrollView的用法,属性
    调整屏幕亮度,调整字体大小
    iOS UIFont 字体名字大全
    ios 6以后,UILabel全属性
    oc中的各种遍历(迭代)方法
    判断app是否是第一次启动
    ios 显示代码块(show the code snippet library)
    ios 添加动画的方法
    添加app第一次启动页面
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4660233.html
Copyright © 2011-2022 走看看