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 }複製代碼