ios開發小技巧之搖一搖截屏

來源:互聯網
上載者:User

1、 監控搖一搖動作   1> 讓當前視圖控制器成為第一響應者     // 必須先讓當前視圖控制器成為第一響應者才能響應動作時間    [self becomeFirstResponder];  2> 實現回應程式法-繼承自UIResponder的方法 複製代碼1 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event2 {3     // 如果動作類型是搖一搖[震動]4     if (motion == UIEventSubtypeMotionShake) {5         6         // 調用截屏方法7         [self snapshot];8     }9 }複製代碼  2、 截屏    注意: 1 > 在擷取映像時,必須先開啟映像上下文,再擷取上下文       2 > 儲存成功後執行的方法必須是固定格式的,也就是下面代碼所展示的格式 複製代碼 1 #pragma mark - 點擊截屏按鈕 2 - (IBAction)snapshot 3 { 4     // 1. 開啟映像上下文[必須先開開啟上下文再執行第二步,順序不可改變] 5     UIGraphicsBeginImageContext(self.view.bounds.size); 6      7     // 2. 擷取上下文 8     CGContextRef context = UIGraphicsGetCurrentContext(); 9     10     // 3. 將當前視圖圖層渲染到當前上下文11     [self.view.layer renderInContext:context];12     13     // 4. 從當前上下文擷取映像14     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();15     16     // 5. 關閉映像上下文17     UIGraphicsEndImageContext();18     19     // 6. 儲存映像至相簿20     UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);21 }22 23 #pragma mark 儲存完成後調用的方法[格式固定]24 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo25 {26     if (error) {27         NSLog(@"error-%@", error.localizedDescription);28     }else{29         NSLog(@"儲存成功");30     }31 }複製代碼

聯繫我們

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