访问相册, 从相册中选取图片

@interface OneViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{

    UIImageView *imageView;

}

@end

@implementation OneViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    

    

    //UIImageView 继承于UIView

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 100, 150)];

    imageView.backgroundColor = [UIColor grayColor];

    [self.view addSubview:imageView];

    

    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];

    button.frame = CGRectMake(100, 200, 100, 100);

    button.layer.cornerRadius = 50;

    [button addTarget:self action:@selector(camera) forControlEvents:(UIControlEventTouchUpInside)];

    [button setTitle:@"相机" forState:(UIControlStateNormal)];

    [self.view addSubview:button];

    button.backgroundColor = [UIColor redColor];

    

    UIButton *mButton = [UIButton buttonWithType:(UIButtonTypeCustom)];

    mButton.frame = CGRectMake(50, 400, 100, 50);

    [mButton addTarget:self action:@selector(photo) forControlEvents:(UIControlEventTouchUpInside)];

    mButton.backgroundColor = [UIColor redColor];

    [mButton setTitle:@"相册" forState:(UIControlStateNormal)];

    [self.view addSubview:mButton];

}

- (void)camera {

    //判断是否由摄像头

    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:(UIImagePickerControllerCameraDeviceRear)];

//UIImagePickerControllerCameraDeviceRear后置摄像头

 //前置摄像头UIImagePickerControllerCameraDeviceFront

    if (!isCamera) {

        NSLog(@"没有后置摄像头");

        return;

    }

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    imagePicker.delegate = self;

    imagePicker.editing = YES;//相机照的照片是否允许编辑

    [self presentViewController:imagePicker animated:YES completion:^{

    }];

    

}

//调取相册

- (void)photo {

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    [self presentViewController:imagePicker animated:YES completion:^{

        NSLog(@"打开系统相册");

    }];

    imagePicker.delegate = self;

    [imagePicker release];

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {

    //选取相册照片

    imageView.image = image;

    [self dismissViewControllerAnimated:YES completion:^{

    }];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/hsxblog/p/4948361.html