美文网首页
图片压缩 UIGraphicsBeginImageContext

图片压缩 UIGraphicsBeginImageContext

作者: 曾柏超 | 来源:发表于2018-07-01 17:57 被阅读13次
    +(UIImage *)zipScaleWithImage:(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 = width*scale;
        //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();
        return newImage;
    }
    
    
    

    相关文章

      网友评论

          本文标题:图片压缩 UIGraphicsBeginImageContext

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