3.5 ios调用系统的相册加载头像
2019-04-19 本文已影响0人
草根小强
ios调用系统的相册加载头像
#import "ViewController.h"
@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (nonatomic, strong) UIImagePickerController * picker;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initImageView];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)initImageView {
//圆角半径裁剪
_imageView.layer.cornerRadius = 30;
//执行裁剪
_imageView.layer.masksToBounds = YES;
_imageView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_imageView.userInteractionEnabled = YES;
UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[self.imageView addGestureRecognizer:tapGR];
[self.view addSubview:self.imageView];
}
- (void)tapped:(UITapGestureRecognizer *)tap {
[self presentViewController:self.picker animated:YES completion:nil];
}
#pragma mark - 协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage * image = info[UIImagePickerControllerOriginalImage];
self.imageView.image = image;
[self.view addSubview:self.imageView];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 懒加载相册
- (UIImagePickerController *)picker {
if (_picker == nil) {
_picker = [[UIImagePickerController alloc] init];
//相册
_picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
_picker.delegate = self;
}
return _picker;
}
@end
#import <UIKit/UIKit.h>
@interface LibraryViewController : UIViewController
@end
#import "LibraryViewController.h"
@interface LibraryViewController ()
@property (nonatomic, strong) UIImagePickerController * picker;
@end
@implementation LibraryViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
@end
ios调用系统的相册加载头像.png