1. 程式人生 > >iOS調取本地相簿更換頭像

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