iOS-图片压缩到指定大小范围内KB、MB

- (void)viewDidLoad {

    [superviewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImageView *imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(10,30, 300,415)];
    imgView.backgroundColor = [UIColoryellowColor];
    imgView.image = [UIImageimageNamed:@"1.jpg"];
    [self.viewaddSubview:imgView];
    //图像压缩
    UIImage *image = [selfscaleFromImage:imgView.image];
    //保存图像
    NSString *strFileName = [selfsaveImage:image];
    imgView.image = image;
    NSLog(@"图片保存路径:%@",strFileName);
}

//==========================
// 图像压缩
//==========================
- (UIImage *)scaleFromImage:(UIImage *)image
{
    if (!image)
    {
        returnnil;
    }
    NSData *data =UIImagePNGRepresentation(image);
    CGFloat dataSize = data.length/1024;
    CGFloat width  = image.size.width;
    CGFloat height = image.size.height;
    CGSize size;

    if (dataSize<=50)//小于50k
    {
        return image;
    }
    elseif (dataSize<=100)//小于100k
    {
        size = CGSizeMake(width/1.f, height/1.f);
    }
    elseif (dataSize<=200)//小于200k
    {
        size = CGSizeMake(width/2.f, height/2.f);
    }
    elseif (dataSize<=500)//小于500k
    {
        size = CGSizeMake(width/2.f, height/2.f);
    }
    elseif (dataSize<=1000)//小于1M
    {
        size = CGSizeMake(width/2.f, height/2.f);
    }
    elseif (dataSize<=2000)//小于2M
    {
        size = CGSizeMake(width/2.f, height/2.f);
    }
    else//大于2M
    {
        size = CGSizeMake(width/2.f, height/2.f);
    }
    NSLog(@"%f,%f",size.width,size.height);
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0,0, size.width, size.height)];
    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if (!newImage)
    {
        return image;
    }
    return newImage;
}

//===============
// 保存图像
//===============
- (NSString *)saveImage:(UIImage*)image
{
    NSData *data;
    if (UIImagePNGRepresentation(image) ==nil)
    {
        data = UIImageJPEGRepresentation(image, 1.0);
    }
    else
    {
        data = UIImagePNGRepresentation(image);
    }
    
    //图片保存的路径
    //这里将图片放在沙盒的documents文件夹中
    NSString * DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
    //文件管理器
    NSFileManager *fileManager = [NSFileManagerdefaultManager];
    //把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
    [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YESattributes:nilerror:nil];
    [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"]contents:data attributes:nil];

    //得到选择后沙盒中图片的完整路径
    NSString *filePath = [[NSStringalloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
    return filePath;
}

 

 

 

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Java中的图像压缩指定大小,可以使用Java的ImageIO和ImageWriter类。以下是一个示例代码,可以将图像压缩指定大小: ```java import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.imageio.ImageWriter; import javax.imageio.stream.FileImageOutputStream; import javax.imageio.stream.ImageOutputStream; public class ImageIOExample { public static void main(String[] args) throws Exception { File input = new File("input.png"); BufferedImage image = ImageIO.read(input); // 限制图像的宽度和高度 int maxWidth = 800; int maxHeight = 600; int width = image.getWidth(); int height = image.getHeight(); if (width > maxWidth || height > maxHeight) { float ratio = Math.min((float)maxWidth / width, (float)maxHeight / height); width = (int)(width * ratio); height = (int)(height * ratio); BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = resized.createGraphics(); g2d.drawImage(image, 0, 0, width, height, null); g2d.dispose(); image = resized; } // 压缩图像 File output = new File("output.png"); ImageWriter writer = ImageIO.getImageWritersByFormatName("png").next(); ImageOutputStream ios = new FileImageOutputStream(output); writer.setOutput(ios); writer.write(null, new javax.imageio.IIOImage(image, null, null), null); ios.close(); writer.dispose(); } } ``` 在这个例子中,我们首先读取一个名为 "input.png" 的图像文件,并将其限制在800x600的大小范围内。如果图像的宽度或高度超过了限制,则缩小图像以适应限制。然后,我们将图像压缩为PNG格式,并将其保存在名为 "output.png" 的文件中。这个例子中的压缩质量是默认的,您可以根据需要更改压缩质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值