iOS開發日記19-7.0之後的截屏方法,ios日記19-7.0截屏

來源:互聯網
上載者:User

iOS開發日記19-7.0之後的截屏方法,ios日記19-7.0截屏

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

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);

}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.