- (void)showUploadSheet {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"上傳" message:@"選擇圖片的方式" preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC addAction:[UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
pickVC.delegate = self;
pickVC.allowsEditing = YES;
pickVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickVC animated:YES completion:nil];
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertVC animated:YES completion:nil];
}
上面的關(guān)鍵在于 pickVC.allowsEditing = YES; 這個(gè)屬性
這個(gè)屬性表示選了圖片以后支持拖動圖片,選擇裁剪范圍
IMG_3377.PNG
同時(shí)要注意的是,選擇完以后一定要Dismiss掉PickerViewController,否則界面會卡住不能操作。
猜測是由于點(diǎn)擊Choose選擇以后,系統(tǒng)自動調(diào)取了編輯頁面的popViewControllerAnimated:方法,(從動畫可以看出,編輯頁面就是通過push的方式壓入棧內(nèi)的),但是編輯頁VC的nav應(yīng)該是由PickerViewController的上一個(gè)VC傳遞過來的,所以如果PickerViewController沒有Dismiss掉,那就會導(dǎo)致界面卡在編輯頁面
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
DLOG(@"image:%@",info);
UIImage *image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}