iOS 图片压缩

  • (UIImage )zipNSDataWithImage:(UIImage )sourceImage{
    //进行图像尺寸的压缩
    CGSize imageSize = sourceImage.size;//取出要压缩的image尺寸
    CGFloat width = imageSize.width; //图片宽度
    CGFloat height = imageSize.height; //图片高度
    //1.宽高大于1280(宽高比不按照2来算,按照1来算)
    if (width>1280||height>1280) {
    if (width>height) {
    CGFloat scale = height/width;
    width = 1280;
    height = width
    scale;
    }else{
    CGFloat scale = width/height;
    height = 1280;
    width = height
    scale;
    }
    //2.宽大于1280高小于1280
    }else if(width>1280||height<1280){
    CGFloat scale = height/width;
    width = 1280;
    height = widthscale;
    //3.宽小于1280高大于1280
    }else if(width<1280||height>1280){
    CGFloat scale = width/height;
    height = 1280;
    width = height
    scale;
    //4.宽高都小于1280
    }else{
    }
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    [sourceImage drawInRect:CGRectMake(0,0,width,height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //进行图像的画面质量压缩
    NSData data=UIImageJPEGRepresentation(newImage, 1.0);
    if (data.length>100
    1024) {
    if (data.length>10241024) {//1M以及以上
    data=UIImageJPEGRepresentation(newImage, 0.1);
    }else if (data.length>512
    1024) {//0.5M-1M
    data=UIImageJPEGRepresentation(newImage, 0.2);
    }else if (data.length>200*1024) {
    //0.25M-0.5M
    data=UIImageJPEGRepresentation(newImage, 0.3);
    }
    }
    return [UIImage imageWithData:data];
    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 图片处理 1.1 编辑图片的几个方法 第一种 先用UIImage对象加载一张图片 然后转化成CGImageRe...
    Kevin_Junbaozi阅读 1,626评论 0 7
  • 两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 N...
    喵喵嘟噜啡阅读 2,051评论 0 9
  • 开发中关于图片处理的地方非常常见,这里分享一下我在处理图片压缩的时候遇到的坑, 图片压缩的2种方法 第一: NSD...
    一时虚荣阅读 393评论 0 0
  • 最近需要用到图片压缩,本来苹果是有一个图片压缩的方法的,但是函数只能说不是很符合我们现在的需求,尤其是一张几M的图...
    Mr_ZH阅读 1,101评论 0 2
  • 提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePi...
    Ashoka_APP阅读 1,533评论 1 1