国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

iOS開發日記19-7.0之后的截屏方法

2019-11-14 18:19:30
字體:
來源:轉載
供稿:網友

今天博主有一個截屏的需求,遇到了一些困難點,在此和大家分享,希望能夠共同進步.

iOS7.0之后廢除了之前常用的截屏方法,也新增了截屏的API.代碼相對簡單,今天就貼出來,大家自行研究.

1.

-(void) screenShot

{

    UIGraphicsBeginImageContext(self.view.bounds.size);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    NSLog(@"image:%@",image);

    UIImageView *imaView = [[UIImageView alloc] initWithImage:image];

    imaView.frame = CGRectMake(0, 0, 100, 100);

    [self.view addSubview:imaView];

     UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

}

 2.

-(void)srceedShot2

{

    //延遲兩秒保存

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

               //獲取圖形上下文

               //    UIGraphicsBeginImageContext(self.view.frame.size);

               UIGraphicsBeginImageContext(self.view.frame.size);

                //將view繪制到圖形上下文中

        

                //    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

                [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

        

             //將截屏保存到相冊

                UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();

        

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

           });}

 

 

//保存至相冊后的回調

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

{

    NSString *msg =nil ;

    if(error){

        msg = @"保存圖片失敗" ;

    }else{

        msg = @"保存圖片成功" ;

    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存圖片結果提示"

                                                   message:msg

                                                  delegate:self

                                         cancelButtonTitle:@"確定"

                                         otherButtonTitles:nil];

    [alert show];

}

 3.

-(void)screenShot3

{

    CGSize size = self.view.bounds.size;

    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

    CGRect rec = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.bounds.size.width, self.view.bounds.size.height);

    [self.view drawViewHierarchyInRect:rec afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    NSData * data = UIImagePNGRePResentation(image);

    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;

    NSString *filename = [path stringByAppendingPathComponent:@"foo.png"];

    [data writeToFile:filename atomically:YES];

    NSLog(@"***********%@",filename);

}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 电白县| 黄山市| 马关县| 贡山| 额济纳旗| 巨野县| 武安市| 呼伦贝尔市| 西城区| 大冶市| 新干县| 广饶县| 宜兰县| 金沙县| 奇台县| 永春县| 永春县| 大姚县| 乐陵市| 邢台县| 铜川市| 平顺县| 清远市| 青浦区| 赤峰市| 长泰县| 沽源县| 湖口县| 南雄市| 南城县| 建始县| 和龙市| 青田县| 云梦县| 石棉县| 永宁县| 中西区| 永善县| 巴塘县| 绵阳市| 新干县|