-(void)addGes{? ? //創(chuàng)建點按手勢??
? UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];? ??
//手勢也可以設置代理??
? tapGes.delegate = self;? ?
?//添加手勢? ?
?[self.imageView addGestureRecognizer:tapGes];? ??
? ? }
// 調(diào)用
- (IBAction)tapGes:(UITapGestureRecognizer *)tapges {??
? [self uploadImage];
}
- (void)uploadImage {? ?
?// 創(chuàng)建并彈出警示框, 選擇獲取圖片的方式(相冊和通過相機拍照)? ?
?UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"獲取圖片"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? message:@"請選擇方式" ? ?preferredStyle:UIAlertControllerStyleActionSheet];? ? ? ??
UIAlertAction *camera = [UIAlertAction actionWithTitle:@"相機" ? ? ? style:UIAlertActionStyleDefault? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? handler:^(UIAlertAction * _Nonnull action) { ??
?[self readImageFromCamera]; ? ? ? ? ? ? ? ? ? ? ??
?? ? ? ? ? ? ? ? ? ? ? ? ? }];??
? UIAlertAction *album = [UIAlertAction actionWithTitle:@"相冊" ? ? ? ? ?style:UIAlertActionStyleDefault? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? handler:^(UIAlertAction * _Nonnull action) {?
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [self readImageFromAlbum]; ? ? ?
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }];??
? UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" ? ? ? ? style:UIAlertActionStyleCancel? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? handler:nil];? ?
?[alertController addAction:camera];?
? [alertController addAction:album];? ??
? [alertController addAction:cancel];? ? ?
? [self presentViewController: alertController animated:YES completion:nil];}
// 從相冊中讀取照片
- (void)readImageFromAlbum {??
?UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; ? ?// 創(chuàng)建一個UIImagePickerController對象 ? ? ? ? ? ?imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;? ? // 選擇資源類型為相冊? ? ?
?? // 指定代理敞嗡,代理需服從,協(xié)議? ?
?imagePicker.delegate = self;? ? ?
?? imagePicker.allowsEditing = YES;? ? // 是否允許用戶編輯? ? ??
? [self presentViewController:imagePicker animated:YES completion:nil]; ?// 展示相冊
}
// 拍照
- (void)readImageFromCamera {??
? ? ? // 判斷選擇的模式是否為相機模式夺衍,如果沒有則彈窗警告??
? if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {??
? ? ? ? ? ? ? UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];? ? ?
?? ? ? ? ? imagePicker.allowsEditing = YES;? ? // 允許編輯? ? ? ? ??
? ? ? imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;? ? ?
?? ? ? ? ? imagePicker.delegate = self;? ? ? ? ? ?
?? ? [self presentViewController:imagePicker animated:YES completion:nil];? ? ? ??
? ? } else {? ? ?
?? // 未檢測到攝像頭彈出窗口? ? ? ? UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? message:@"未檢測到攝像頭"? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? preferredStyle:UIAlertControllerStyleAlert];? ? ? ? ??
? ? ? UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];? ? ? ??
? ? ? ? [alert addAction:confirm];? ? ? ??
? ? ? ? [self presentViewController:alert animated:YES completion:nil];? ? ??
? ? ? }?
?? }
#pragma mark// 圖片編輯完成之后觸發(fā)的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
// 1. 讓image顯示在界面上即可
self.imageView.image = image;
//圖片存入相冊
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
[self dismissViewControllerAnimated:YES completion:nil];
}