iOS调取本地相册更换头像

把调取相册获取图片与controller分离开,写成一个单独的类,在这里命名为NOVSelectPhotoManager

.h文件中

//声明一个协议NOVSelectPhotoManagerDeleagte
@protocol NOVSelectPhotoManagerDeleagte <NSObject>

@optional

//完成图片的选择时执行该协议方法
-(void)didChooseImage:(UIImage *)image;

@end

@interface NOVSelectPhotoManager : NSObject<UIImagePickerControllerDelegate>

@property(nonatomic,weak) id<NOVSelectPhotoManagerDeleagte> deleagte;

-(instancetype)initWithViewController:(UIViewController *)viewController;
//声明以下方法
//从相册中选取
-(void)selectImageWithAlbum;

//拍照
-(void)selectImageWithCamera;

@end

.m文件

  • 声明一个viewcontroller对象(完成图片的选择时需要跳转回去的那个controller,也就是需要更换图片的界面)
@implementation NOVSelectPhotoManager{
    UIViewController *presentController;
}

-(instancetype)initWithViewController:(UIViewController *)viewController{
    self = [super init];
    if (self) {
        //在初始化方法中给presentController赋值
        presentController = viewController;
    }
    return self;
}
  • 实现从相册中选取图片,进入图片选择界面,选择好图片后,自动执行UIImagePickerController的代理方法imagePickerController:didFinishPickingMediaWithInfo:
-(void)selectImageWithAlbum{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    //允许编辑
    imagePickerController.allowsEditing = YES;
    //设置sourceTypeUIImagePickerControllerSourceTypePhotoLibrary,代表调取相册
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if (presentController) {
        //从需要更换图片的界面跳转到选择图片界面
        [presentController presentViewController:imagePickerController animated:YES completion:nil];
        //选择完成后执行代理方法imagePickerController:idFinishPickingMediaWithInfo:
    }
}
  • UIImagePickerController的代理方法,图片选择完成后执行该方法,可以从该代理方法中获取到所选择的图片信息。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //获取所选择的图片
    UIImage *image = info[@"UIImagePickerControllerEditedImage"];
    if (!image) {
        image = info[@"UIImagePickerControllerOriginalImage"];
    }
    if (_deleagte && [_deleagte respondsToSelector:@selector(didChooseImage:)]) {
        //获取到图片之后执行该方法,并将图片作为参数传给需要更换图片的viewcontroller
        [self.deleagte didChooseImage:image];
    }
    //跳转会需要更换图片的界面
    [picker dismissViewControllerAnimated:YES completion:nil];
}
info是字典类型,记录了所选择图片的信息,这里选择了UIImagePickerControllerEditedImage,也就是编辑之后的图片,若为空则取UIImagePickerControllerOriginalImage,即为原图片。
  • 拍照(调取相机)
-(void)selectImageWithCamera{
    if (![UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
        NSLog(@"no camera");
        return;
    }
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    [imagePickerController dismissViewControllerAnimated:YES completion:nil];
}
  • viewcontroller 中
@interface MYViewController ()<NOVSelectPhotoManagerDeleagte>//实现
//声明一个调取相册的类
@property(nonatomic,strong) NOVSelectPhotoManager *photoManager;
@end
  • 在viewcontroller添加UIAlertController,分别执行photograph,selectFromAlbum方法
    _actionController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *photographAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //相机
        [self photograph];
    }];
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:@"从相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //相册
        [self selectFromAlbum];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [_actionController addAction:photographAction];
    [_actionController addAction:albumAction];
    [_actionController addAction:cancelAction];
  • 实现方法selectFromAlbum
-(void)selectFromAlbum{
    if (!_photoManager) {
        //初始化并设置代理
        _photoManager = [[NOVSelectPhotoManager alloc] initWithViewController:self];
        _photoManager.deleagte = self;
    }
    //调用方法进行图片的选择
    [_photoManager selectImageWithAlbum];
}
  • 实现方法photograph
 if (!_photoManager) {
        _photoManager = [[NOVSelectPhotoManager alloc] initWithViewController:self];
        _photoManager.deleagte = self;
    }
    [_photoManager selectImageWithCamera];
  • 实现NOVSelectPhotoManager的代理方法didChooseImage:,并根据传过来的image更换图片
-(void)changeImageWithImage:(UIImage *)image{
    NSLog(@"=====%@",image);
    [_myView.headview.myImageButton setImage:image forState:UIControlStateNormal];
}

猜你喜欢

转载自blog.csdn.net/qq_39696600/article/details/80559613
今日推荐