美文网首页
手机拍照或者相册选取图片缩放做头像

手机拍照或者相册选取图片缩放做头像

作者: 简书笔记存档 | 来源:发表于2016-06-22 14:59 被阅读0次
    • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
      [picker dismissViewControllerAnimated:YES completion:^{}];
      UIImage *photo = info[UIImagePickerControllerOriginalImage];

      if ((photo.size.width > 100) || photo.size.height > 100 ) {
      UIImage * smallImage = [self scaleFromImage:photo toSize:CGSizeMake(100, 100)];
      self.request = [HHUtils uploadImage:smallImage delegate:self];

        [picker dismissViewControllerAnimated:YES completion:nil];
      

      }else{
      // UIImage *photo = [UIImage imageNamed:@"cf_QQ"];
      self.request = [HHUtils uploadImage:photo delegate:self];

      [picker dismissViewControllerAnimated:YES completion:nil];
      }
      }

    • (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

      [picker dismissViewControllerAnimated:YES completion:nil];
      }

    pragma mark ----------- 改变图像的尺寸,方便上传服务器

    • (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize) size

      UIGraphicsBeginImageContext(size);
      [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
      UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      return newImage;
      }

    • (UIImage *)thumbnailWithImageWithoutScale:(UIImage )image size:(CGSize)asize{
      UIImage newimage;
      if (nil == image) {
      newimage = nil;
      }else{
      CGSize oldsize = image.size;
      CGRect rect;
      if (asize.width/asize.height > oldsize.width/oldsize.height) {
      rect.size.width = asize.height
      oldsize.width/oldsize.height;
      rect.size.height = asize.height;
      rect.origin.x = (asize.width - rect.size.width)/2;
      rect.origin.y = 0;
      }else{
      rect.size.width = asize.width;
      rect.size.height = asize.width
      oldsize.height/oldsize.width;
      rect.origin.x = 0;
      rect.origin.y = (asize.height - rect.size.height)/2;
      }
      UIGraphicsBeginImageContext(asize);
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
      UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
      [image drawInRect:rect];
      newimage = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      }
      return newimage;
      }

    相关文章

      网友评论

          本文标题:手机拍照或者相册选取图片缩放做头像

          本文链接:https://www.haomeiwen.com/subject/gxcndttx.html