1. 程式人生 > >開啟系統相簿/相機,選擇圖片

開啟系統相簿/相機,選擇圖片

1.實現2個代理<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

2.

/**
 *  開啟相機
 */
- (void)openCamera
{
    UIImagePickerController *pic = [[UIImagePickerController alloc] init];
    pic.sourceType = UIImagePickerControllerSourceTypeCamera;
    pic.delegate = self;
    [self presentViewController:pic animated:YES completion:nil];
}

/**
 *  開啟相簿
 */
- (void)openPhotoLibrary
{
    UIImagePickerController *pic = [[UIImagePickerController alloc] init];
    pic.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pic.delegate = self;
    [self presentViewController:pic animated:YES completion:nil];
}

#pragma mark - 圖片選中控制器的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //1.銷燬picker控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    //2.取得圖片
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.imageView.image = image;
}