更换图片的方法有很多,现在对其中一种方式进行学习,加深自己的知识点.
一.更换图片
利用手势点击图片调用系统相机和系统相册更换图片的需求简单处理.
1.展示一张本地图片的代码
//设置图像的一些属性,添加手势
- (void)setImageViewConfiguration{
self.changeImage = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 100) / 2, 100, 100, 100)];
//把图片设置成圆形。 我这里在故事版里面设置的imageView是一个正方形(因为头像图片都是放在正方形的imageView里)
//裁成圆角
self.changeImage.layer.cornerRadius = self.changeImage.frame.size.width/2;
//隐藏裁剪掉的部分
self.changeImage.layer.masksToBounds = YES;
self.changeImage.layer.borderWidth = 2.0f;
self.changeImage.layer.borderColor = [UIColor redColor].CGColor;
self.changeImage.image = [UIImage imageNamed:@"timg.png"];
[self.view addSubview:self.changeImage];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 140) / 2, 250, 140, 50)];
label.text = @"点击图片更换图像";
[self.view addSubview:label];
/**
* 添加手势:当点击图片的时候,弹出手势对应的事件
*
*/
//允许用户交互
_changeImage.userInteractionEnabled = YES;
//初始化一个手势
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alterHeadPortrait:)];
//添加手势
[_changeImage addGestureRecognizer:singleTap];
}
效果如图:
2.点击事件的执行,通过代理调用系统相机或者系统相册
//点击事件的执行
- (void)alterHeadPortrait:(UIGestureRecognizer *)gesture{
/**
*通过UIAlertController添加弹出提示框
*/
//初始化提示框
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//从相册选择,类型:UIAlertActionStyleDefault
[alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//初始化UIImagePickerController
UIImagePickerController *PickerImage = [[UIImagePickerController alloc] init];
//获取方式1, 通过相册(呈现全部相册), UIImagePickerControllerSourceTypePhotoLibrary
//获取方式2, 通过相机,UIImagePickerControllerSourceTypeCamera
//获取方法3, 通过相册(呈现全部图片),UIImagePickerControllerSourceTypeSavedPhotosAlbum
PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//允许编辑,即放大裁剪
PickerImage.allowsEditing = YES;
//自代理
PickerImage.delegate = self;
//页面跳转
[self presentViewController:PickerImage animated:YES completion:nil];
}]];
//拍照,类型:UIAlertActionStyleDefault
[alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
/**
其实和从相册选择一样,只是获取方式不同,前面是通过相册,而现在,我们要通过相机的方式
*/
UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
//获取方式:通过相机
PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
PickerImage.allowsEditing = YES;
PickerImage.delegate = self;
[self presentViewController:PickerImage animated:YES completion:nil];
}]];
//取消,类型:UIAlertActionStyleCancel
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[self presentViewController:alert animated:YES completion:nil];
}
提示框效果如图:
3.图片更换成功后将更换后的图片展示在UIImageView上
//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//定义一个newPhoto,用来存放我们选择的图片。
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
_changeImage.image = newPhoto;
[self dismissViewControllerAnimated:YES completion:nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
[UIImagePNGRepresentation(newPhoto)writeToFile: filePath atomically:YES];
//保存照片到本地
[self dicPaths];
}
更换图片之后效果:
二.保存到本地
1.将图片写入本地
//保存图片信息到本地
-(void)dicPaths
{
NSMutableArray *specialArr = [[NSMutableArray alloc] initWithCapacity:0];
self.dic = [[NSMutableDictionary alloc]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
NSLog(@"保存的路径:%@", filePath);
[self.dic setObject:filePath forKey:@"img"];
[specialArr addObject:self.dic];
}
2.从本地读取照片信息
//从本地获取图片 并赋值
-(void)plist
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
[_changeImage setImage:img];
}
三.保存到服务端
图片的读取首先是在缓存中去取(此demo均为本地图片暂时不考虑,下篇将对网络请求图片进行述说),倘若没有的话需要到本地磁盘中获取,若本地磁盘任然没有找到就从网络上获取,即保存到服务端.