在我们的开发中,有的app需要从系统相册中获取图片,如设置用户头像等,下面分享这个功能的实现,先上代码:
#import "ViewController.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *IconView;
@end
@implementation ViewController
- (IBAction)chooseImage {
//弹出系统相册
UIImagePickerController *pickVC = [[UIImagePickerController alloc] init];
//设置照片来源
pickVC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
pickVC.delegate = self;
[self presentViewController:pickVC animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *photo = info[UIImagePickerControllerOriginalImage];
UIImageView *imageV = [[UIImageView alloc] init];
imageV.frame = self.IconView.frame;
imageV.image = photo;
imageV.userInteractionEnabled = YES;
[self.view addSubview:imageV];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end