一.获取单张图片
1.利用UIImagePickerController可以从系统自带的App(照片\相机)中获得图片
2.设置代理,遵守代理协议
注意:这个UIImagePickerController类比较特殊,需要遵守两个代理协议
<UIImagePickerControllerDelegate><UINavigationControllerDelegate>
代码如下:
@interface ResumeViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,strong)UIImagePickerController *imagePicker;
@property(nonatomic,strong)UIImageView *photoView;
@property(nonatomic,strong)UIImageView *addImage;
@property(nonatomic,strong)UILabel *photoL;
@end
#pragma mark - 头像访问相册,相机
self.imagePicker = [[UIImagePickerController alloc]init];
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = YES;
#pragma mark --- 照片
self.photoView = [[UIImageView alloc]init];
self.photoView.backgroundColor = [UIColor whiteColor];
[self.scrollView addSubview:self.photoView];
self.photoView.userInteractionEnabled = YES;
[self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.scrollView.mas_top);
make.right.equalTo(self.view.mas_right).offset(-5);
make.width.mas_offset(140*ScreenWidth/375);
make.height.mas_offset(46*4*ScreenWidth/375);
}];
self.photoView.layer.borderWidth = 1;
self.photoView.layer.borderColor = [UIColor colorWithHexString:@"#e1e1e1"].CGColor;
//给图片添加点击事件
UITapGestureRecognizer *tapPicture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedImageForIcon)];
[self.photoView addGestureRecognizer:tapPicture];
//
self.addImage = [[UIImageView alloc]init];
[self.photoView addSubview:self.addImage];
self.addImage.image = [UIImage imageNamed:@"填写简历-基本_03"];
self.addImage.userInteractionEnabled = YES;
[self.addImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.photoView.mas_centerX);
make.top.equalTo(self.photoView.mas_top).offset(60*ScreenWidth/375);
make.width.mas_offset(20*ScreenWidth/375);
make.height.mas_offset(20*ScreenWidth/375);
}];
self.photoL = [[UILabel alloc]init];
self.photoL.backgroundColor = [UIColor whiteColor];
self.photoL.text = @"添加照片";
self.photoL.font = [UIFont systemFontOfSize:19*ScreenWidth/375];
self.photoL.textColor = [UIColor colorWithHexString:@"#888888"];
[self.photoView addSubview:self.photoL];
[self.photoL mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.photoView.mas_centerX);
make.top.equalTo(self.addImage.mas_bottom).offset(20*ScreenWidth/375);
}];
//从相册,图库,相机获取图片
- (void)selectedImageForIcon{
// NSLog(@"aaaaaaaaaaa");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}];
UIAlertAction *actionPhotoLibrary = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:actionCamera];
[alertController addAction:actionPhotoLibrary];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil]; }
//从相册选择图片后操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info{
//获取裁剪后的图片
UIImage *image = info[UIImagePickerControllerEditedImage];
//将照片存到媒体库
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
self.photoView.image = image;
//将照片存到沙盒
[self saveImage:image];
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
//照片存到本地后的回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
if (!error) {
NSLog(@"存储成功");
self.addImage.hidden = YES;
self.photoL.hidden = YES;
}else{
NSLog(@"存储失败:%@",error);
}
}
//保存图片
- (void)saveImage:(UIImage *)currentImage{
//设置照片的品质
NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
NSLog(@"%@",NSHomeDirectory());
//获取沙盒目录
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/currentImage.png"];
//将图片写入文件
[imageData writeToFile:filePath atomically:NO];
}
//取消操作
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
做到这里,运行程序,点击相册或相机会崩溃,是因为没有在info.plist文件中添加这两个,填上就好了