iOS開發UI篇—Quartz2D使用(截屏)

來源:互聯網
上載者:User

標籤:

iOS開發UI篇—Quartz2D使用(截屏)

一、簡單說明

在程式開發中,有時候需要截取螢幕上的某一塊內容,比如捕魚達人遊戲。

完成截屏功能的核心代碼:- (void)renderInContext:(CGContextRef)ctx;調用某個view的layer的renderInContext:方法即可

二、程式碼範例

  storyboard介面搭建:

 

代碼:

////  YYViewController.m//  01-截屏////  Created by apple on 14-6-12.//  Copyright (c) 2014年 itcase. All rights reserved.//#import "YYViewController.h"#import "MBProgressHUD+NJ.h"@interface YYViewController ()@property (weak, nonatomic) IBOutlet UIView *contentView;- (IBAction)BtnClick:(UIButton *)sender;@end@implementation YYViewController- (void)viewDidLoad{    [super viewDidLoad];}- (IBAction)BtnClick:(UIButton *)sender {        //延遲兩秒儲存    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        //擷取圖形上下文        //    UIGraphicsBeginImageContext(self.view.frame.size);        UIGraphicsBeginImageContext(self.contentView.frame.size);        //將view繪製到圖形上下文中                //    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];        [self.contentView.layer renderInContext:UIGraphicsGetCurrentContext()];                     //將截屏儲存到相簿        UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();                UIImageWriteToSavedPhotosAlbum(newImage,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);    });} - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{    if (error) {        [MBProgressHUD showError:@"儲存失敗,請檢查是否擁有相關的許可權"];    }else    {//        [MBProgressHUD showMessage:@"儲存成功!"];        [MBProgressHUD showSuccess:@"儲存成功!"];    }}@end
把截取的圖片儲存到手機的相簿中:

 

說明:把整個螢幕畫到一張圖片裡1.建立一個bitmap的上下文2.將螢幕繪製帶上下文中3.從上下文中取出繪製好的圖片4.儲存圖片到相簿 補充:把圖片寫入到檔案的代碼
//3.從上下文中取出繪製好的圖片     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();          NSData *data = UIImagePNGRepresentation(newImage);          NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"abc.png"];     NSLog(@"%@", path);     [data writeToFile:path atomically:YES];
三、補充儲存成功和儲存失敗之後應該做些事情?系統推薦的方法:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{    if (error) {        [MBProgressHUD showError:@"儲存失敗,請檢查是否擁有相關的許可權"];    }else    {//        [MBProgressHUD showMessage:@"儲存成功!"];        [MBProgressHUD showSuccess:@"儲存成功!"];    }}
如果圖片成功儲存的話,那麼就提示儲存成功。如果儲存失敗,那麼提示失敗提示:儲存失敗常見有兩個原因:1是記憶體不夠,2是手機內部的許可權不允許。說明:如果當一個應用程式想要訪問通訊錄或相簿,使用者已經明確拒絕過,那麼以後再要訪問的話會直接拒絕。這個時候,可以提示使用者去開啟許可權。

iOS開發UI篇—Quartz2D使用(截屏)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.