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

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

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

一、簡單說明

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

 

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

二、程式碼範例

  storyboard介面搭建:

代碼:

 1 // 2 //  YYViewController.m 3 //  01-截屏 4 // 5 //  Created by apple on 14-6-12. 6 //  Copyright (c) 2014年 itcase. All rights reserved. 7 // 8  9 #import "YYViewController.h"10 #import "MBProgressHUD+NJ.h"11 12 @interface YYViewController ()13 @property (weak, nonatomic) IBOutlet UIView *contentView;14 - (IBAction)BtnClick:(UIButton *)sender;15 16 @end17 18 @implementation YYViewController19 20 - (void)viewDidLoad21 {22     [super viewDidLoad];23 }24 25 - (IBAction)BtnClick:(UIButton *)sender {26     27     //延遲兩秒儲存28     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{29         //擷取圖形上下文30         //    UIGraphicsBeginImageContext(self.view.frame.size);31         UIGraphicsBeginImageContext(self.contentView.frame.size);32         //將view繪製到圖形上下文中33         34         //    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];35         [self.contentView.layer renderInContext:UIGraphicsGetCurrentContext()];36      37         38         //將截屏儲存到相簿39         UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();40         41         UIImageWriteToSavedPhotosAlbum(newImage,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);42     });43 }44 45  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo46 {47     if (error) {48         [MBProgressHUD showError:@"儲存失敗,請檢查是否擁有相關的許可權"];49     }else50     {51 //        [MBProgressHUD showMessage:@"儲存成功!"];52         [MBProgressHUD showSuccess:@"儲存成功!"];53     }54 }55 56 @end
把截取的圖片儲存到手機的相簿中:說明:把整個螢幕畫到一張圖片裡1.建立一個bitmap的上下文2.將螢幕繪製帶上下文中3.從上下文中取出繪製好的圖片4.儲存圖片到相簿 補充:把圖片寫入到檔案的代碼
1 //3.從上下文中取出繪製好的圖片2      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();3      4      NSData *data = UIImagePNGRepresentation(newImage);5      6      NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"abc.png"];7      NSLog(@"%@", path);8      [data writeToFile:path atomically:YES];
三、補充儲存成功和儲存失敗之後應該做些事情?系統推薦的方法:
 1  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 2 { 3     if (error) { 4         [MBProgressHUD showError:@"儲存失敗,請檢查是否擁有相關的許可權"]; 5     }else 6     { 7 //        [MBProgressHUD showMessage:@"儲存成功!"]; 8         [MBProgressHUD showSuccess:@"儲存成功!"]; 9     }10 }
如果圖片成功儲存的話,那麼就提示儲存成功。如果儲存失敗,那麼提示失敗提示:儲存失敗常見有兩個原因:1是記憶體不夠,2是手機內部的許可權不允許。說明:如果當一個應用程式想要訪問通訊錄或相簿,使用者已經明確拒絕過,那麼以後再要訪問的話會直接拒絕。這個時候,可以提示使用者去開啟許可權。
相關文章

聯繫我們

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