iOS相冊圖片的選取與設置

我的模塊中常常用戶的頭像可以用戶自定義自己的頭像,可以選擇拍照也可以選擇從用戶的自己的相冊中選擇,具體的實現代碼如下:

@interface PickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>

//名字
@property (weak, nonatomic) IBOutlet UILabel *namelabel;
//頭像
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
//actionsheet底部彈窗
@property (nonatomic,strong) UIActionSheet *actionSheet;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.namelabel.text = @"XXX";
    self.iconView.image = [UIImage imageNamed:@"IMG_1743"];
    self.iconView.layer.masksToBounds = YES;
    self.iconView.layer.cornerRadius = 40;
    self.iconView.userInteractionEnabled = YES;
    //給頭像添加點擊手勢
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [self.iconView addGestureRecognizer:ges];
- (void)tapClick {
    NSLog(@"打開相冊");
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"從相冊選取", nil];
    }else {
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"選擇圖像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"從相冊選擇",nil];
    }
    self.actionSheet.tag = 1000;
    [self.actionSheet showInView:self.view];
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (actionSheet.tag == 1000) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判斷是否支持相機
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //來源:相機
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    //來源:相冊
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
            }
        }
        else {
            if (buttonIndex == 2) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳轉到相機或相冊頁面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:^{
            
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.iconView.image = image;
//    self.namelabel.text = image.description;
}



發佈了84 篇原創文章 · 獲贊 23 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章