一.压缩图片的大小
//压缩图片
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
二.计算图片大小
NSData * imageDa = UIImageJPEGRepresentation(newPhoto,1);
CGFloat leng = [imageDa length]/1024;
NSLog(@"大小 ==== %f",leng);
newPhoto = [self scaleToSize:newPhoto size:CGSizeMake(50, 50)];
NSData * imageData = UIImageJPEGRepresentation(newPhoto,1);
CGFloat length = [imageData length]/1024;
NSLog(@"后面的大小 ==== %f",length);
三.从相册或拍照中选择图片
要在plist中设置权限:
//相册
NSPhotoLibraryUsageDescription
App需要您的同意,才能访问相册
//相机
NSCameraUsageDescription
App需要您的同意,才能访问相机
#pragma mark 选择图片
// 方法:设置头像样式
-(void)setHeadPortrait{
// 把头像设置成圆形
self.headPortraitImgView.layer.cornerRadius=self.headPortraitImgView.frame.size.width/2;
self.headPortraitImgView.layer.masksToBounds=YES;
// 给头像加一个圆形边框
self.headPortraitImgView.layer.borderWidth = 1.5f;
self.headPortraitImgView.layer.borderColor = [UIColor whiteColor].CGColor;
// self.headerImgView.image = [UIImage imageNamed:@"mytx.jpg"];
/**
* 添加手势:也就是当用户点击头像了之后,对这个操作进行反应
*/
//允许用户交互
self.headPortraitImgView.userInteractionEnabled = YES;
//初始化一个手势
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(alterHeadPortrait:)];
//给imageView添加手势
[self.headPortraitImgView addGestureRecognizer:singleTap];
}
// 方法:alterHeadPortrait
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
/**
* 弹出提示框
*/
//初始化提示框
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];
}
//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
//定义一个newPhoto,用来存放我们选择的图片。
UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
NSLog(@"回调图片 === %@",newPhoto);
NSLog(@"回调图片 === %@",info);
NSData * imageDa = UIImageJPEGRepresentation(newPhoto,1);
CGFloat leng = [imageDa length]/1024;
NSLog(@"大小 ==== %f",leng);
newPhoto = [self scaleToSize:newPhoto size:CGSizeMake(50, 50)];
NSData * imageData = UIImageJPEGRepresentation(newPhoto,1);
CGFloat length = [imageData length]/1024;
NSLog(@"后面的大小 ==== %f",length);
// self.headerImgView.image = newPhoto;
self.headPortraitImgView.image = newPhoto;
[self dismissViewControllerAnimated:YES completion:nil];
}