在我们需要访问UIImagePickerController的时候,一般为选择图片,此时我们可能需要做如下操作:
一、访问相册、图库、拍照功能;
二、图片设置;
1、需要将选择的图片缩小 (等比例);
2、设置图片的形状 (比如:希望选择的图片为圆形);
3、将图片翻转一定的角度 (比如翻转90度、180度等)
以下是我自己的总结的一些方法:
一、访问功能
- (IBAction)changeIconAction:(UITapGestureRecognizer *)sender { // UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册",@"图库", nil]; // [actionSheet showInView:[UIApplication sharedApplication].keyWindow]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { dispatch_async(dispatch_get_main_queue(), ^{ [self makePhoto]; }); }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { dispatch_async(dispatch_get_main_queue(), ^{ [self choosePicture]; }); }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { dispatch_async(dispatch_get_main_queue(), ^{ [self pictureLibrary]; }); }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } //跳转到imagePicker里 - (void)makePhoto{ pickerController = [[UIImagePickerController alloc] init]; pickerController.allowsEditing = YES; pickerController.delegate = self; pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:pickerController animated:YES completion:nil]; } //跳转到相册 - (void)choosePicture { pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self presentViewController:pickerController animated:YES completion:nil]; } //跳转图库 - (void)pictureLibrary { pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:pickerController animated:YES completion:nil]; } //用户取消退出picker时候调用 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { NSLog(@"%@",picker); [pickerController dismissViewControllerAnimated:YES completion:^{ }]; } //用户选中图片之后的回调 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"%s,info == %@",__func__,info); UIImage *userImage = [self fixOrientation:[info objectForKey:@"UIImagePickerControllerOriginalImage"]]; userImage = [self scaleImage:userImage toScale:0.3]; //保存图片 // [self saveImage:userImage name:@"某个特定标示"]; [pickerController dismissViewControllerAnimated:YES completion:^{ }]; [self.headIcon setImage:userImage]; self.headIcon.contentMode = UIViewContentModeScaleAspectFill; self.headIcon.clipsToBounds = YES; //照片上传 [self upDateHeadIcon:userImage]; }