MinggeQingchun的博客

记录就是一种积累,积累就是一种提高,提高就是一种成长!

图片压缩到指定大小范围内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;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/MinggeQingchun/article/details/51539355
文章标签: 图片压缩
个人分类: iOS-OC
上一篇如何使用Xcode的Targets来管理开发和生产版本的构建
下一篇iOS自定义日期并进行卡控
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭