#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
添加代理
UIImagePickerControllerDelegate,UINavigationControllerDelegate
//定义类
@property (nonatomic ,strong) UIImagePickerController *imagePickerController;
#pragma mark -调取相机实现方法
- (void)launchImagePicker {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"更改头像"
message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *fromCamera = [UIAlertAction actionWithTitle:@"拍照"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action)
{
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePickerController.allowsEditing = YES;
[self presentViewController:self.imagePickerController
animated:YES
completion:nil];
}];
fromCamera.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
UIAlertAction *fromGallery = [UIAlertAction actionWithTitle:@"从手机相册选取"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull
action)
{
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.imagePickerController
animated:YES
completion:nil];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:nil];
[alertController addAction:fromGallery];
[alertController addAction:fromCamera];
[alertController addAction:cancel];
[self presentViewController:alertController
animated:YES
completion:nil];
}
#pragma mark - ImagePicker Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
//判断资源类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
UIImage *userAvatar;
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
userAvatar = [info objectForKey:UIImagePickerControllerOriginalImage];
} else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
userAvatar = [info objectForKey:UIImagePickerControllerEditedImage];
}
[self.Cell.imgView setImage:userAvatar];
//压缩图片
// NSData *fileData = UIImageJPEGRepresentation(userAvatar, 1.0);
// //保存图片至相册
// UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
//上传图片网络请求
[self getRequest:userAvatar];
}else{
[self showMessage:@"不能上传视频"];
}
}
#pragma mark 图片保存完毕的回调
- (void) image: (UIImage *) image didFinishSavingWithError:(NSError *)error
contextInfo: (void *)contextInf{
LHLog(@"图片保存成功");
}
#pragma mark 视频保存完毕的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error
contextInfo:(void *)contextInf{
if (error) {
LHLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription);
}else{
LHLog(@"视频保存成功.");
}
}
- (UIImagePickerController *)imagePickerController {
if (!_imagePickerController) {
_imagePickerController = [[UIImagePickerController alloc] init];
[_imagePickerController setDelegate:self];
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[_imagePickerController setAllowsEditing:YES];
}
return _imagePickerController;
}
第二种
//添加代理
UIActionSheetDelegate,UIImagePickerControllerDelegate
//定义类
@property (strong, nonatomic) UIActionSheet *imageActionSheet;
#pragma mark 调取相机
- (void)launchImagePicker{
UIActionSheet *imageActionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照",@"用户相册", nil];
self.imageActionSheet = imageActionSheet;
[imageActionSheet showInView:self.view];
}
#pragma mark UIActionSheetDelegate 修改头像选择
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (actionSheet == self.imageActionSheet) {
// 拍照
if (buttonIndex == 0) {
// 判断摄像头是否可用
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
// 初始化
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:imagePicker animated:YES completion:nil];
} else { //摄像头不可用
}
// 从相册选择
} else if (buttonIndex == 1) {
// 判断是否能读取相册
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:imagePicker animated:YES completion:nil];
} else { //用户相册不可用
}
}
}
}
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *avatarImage = [info objectForKey:UIImagePickerControllerEditedImage];
[self.Cell.imgView setImage:avatarImage];
// 压缩图片
// NSData *fileData = UIImageJPEGRepresentation(avatarImage, 1.0);
//保存图片至相册
// UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
[self dismissViewControllerAnimated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
//选择过之后自动执行网络请求
[self getRequest:avatarImage];
}];
}