两种上传头像的方式(file文件)

5 篇文章 0 订阅


#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];

    }];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值