图片处理

一.压缩图片的大小

//压缩图片
- (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];

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小毅哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值