兩種上傳頭像的方式(file檔案)
#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];
}];
}