关于压缩图片不失真的方法

很多时候我们会向服务器上传照片,如果选择照片直接上传的话,势必会造成时间和流量的浪费,也会直接影响用户的体验,此时就需要我们对图片进行压缩了.但是,压缩图片又会造成图片的失真,使图片看起来模糊不清,所以就会很郁闷,此处我总结了一下,可以把图片压缩到10k左右并且不会失真,在服务器端查看图片一样清晰(已经在自己公司的服务器测试过,没有问题,讲1.5M的图片压缩到了12k),分享一下,希望可以帮助到受此困惑的朋友,如果有更好的方法,也希望可以互相学习!

-(void)viewDidLoad {
  //此处写一个按钮,然后在按钮的点击事件里写这个UIActionSheet,调用相册和相机,比较简单,就不写了
  UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"打开相机"@"打开相册", nil];
[action showInView:self.view];
}

//这是actionSheet的代理方法  调用相册和相机的用法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {//相机
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        //            [picker release];
        [self presentModalViewController:picker animated:YES];
    }
}else if (buttonIndex ==1) {//相册
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
   picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
   picker.delegate = self;
  //设置选择后的图片可被编辑
   picker.allowsEditing = YES;
  [self presentModalViewController:picker animated:YES];

 }
}

//这是UIImagePickerControllerDelegate代理方法 这是重点 我也会重点注释
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

//当选择的类型是图片
if ([type isEqualToString:(NSString*)kUTTypeImage])
{
    //先把图片转成NSData
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    //设置image的尺寸
    CGSize imagesize = image.size;
    imagesize.height = 500;
    imagesize.width = 370;
    //对图片大小进行压缩--
    image = [self imageWithImage:image scaledToSize:imagesize];
    
    UIImageOrientation *imageOrientation = image.imageOrientation;
    if (imageOrientation != UIImageOrientationUp) {
        // 原始图片可以根据照相时的角度来显示,但UIImage无法判定,于是出现获取的图片会向左转90度的现象。
        // 以下为调整图片角度的部分
        UIGraphicsBeginImageContext(image.size);
        [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        // 调整图片角度完毕
    }
    
    NSData *data;
    data = UIImageJPEGRepresentation(image, 0.5);//这句话就是压缩图片的比例
    
    NSInteger length = [data length]/1000;//这就是打印图片的大小
    NSLog(@"length:%ld",(long)length);

    //图片保存的路径(这些就是固定的用法了,不做详述)
    //这里将图片放在沙盒的documents文件夹中
    NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    
    //文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
    [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
    [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
    
    //得到选择后沙盒中图片的完整路径
    self.filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
    
    //关闭相册界面
    [picker dismissModalViewControllerAnimated:YES];
    
    //显示图片(下面就是你要显示图片的代码了,此时上传的时候图片的大小已经改变了,可以在上面打印图大小的地方看到)
    
}

//对图片尺寸进行压缩--
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
  // Create a graphics image context
  UIGraphicsBeginImageContext(newSize);

  // Tell the old image to draw in this new context, with the desired
  // new size
  [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

  // Get the new image from the context
  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

  // End the context
  UIGraphicsEndImageContext();

  // Return the new image.
  return newImage;
}

你可能感兴趣的:(关于压缩图片不失真的方法)