关于图片压缩尺寸和大小

距离上次博客时间已经有十天左右,过了那么久才有时间来写博客,顿时觉得坚持还是很困难的!!!

这次开发中遇到一个很头疼的问题,是图片压缩的问题,先说下我们的要求,我们要求图片要在600x400的尺寸,如果在这个范围内就不作处理,如果比这个尺寸大,就要改变尺寸大小。同时要保持图片的大小要在50K之内。。。

废话不说了上代码,请路过的大神来指点下,因为我的图片问题还未完全解决,上传的时候有的会被压缩到80K左右,还是要改善代码的。。。。

 

+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize 

{

  //image是需要改变的image,asize是需要改变的尺寸,我们的是600x400.

    UIImage *newImage = nil;

    CGSize imageSize = image.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    CGFloat targetWidth = asize.width;

    CGFloat targetHeight = asize.height;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

    CGFloat sacleWidth = 0.0;

    CGFloat sacleHeigh = 0.0;

    if(CGSizeEqualToSize(imageSize, asize) == NO){

        CGFloat widthFactor = targetWidth / width;

        CGFloat heightFactor = targetHeight / height;

        CGFloat asizeFactor = targetWidth / targetHeight;

        CGFloat sizeFactor = width / height;

        //如果比例比现有比例高

        if(sizeFactor > asizeFactor){

            if(width < targetWidth){

                //高度比标准小  宽度必定比标准小

                sacleWidth = width;

                sacleHeigh = height;

            }else{

                //宽度比标准大 计算高度

                sacleWidth = 400;

                sacleHeigh = height / (width / targetWidth);

            }

        }

        else{

            if(height < targetHeight){

                //高度比标准小  宽度必定比标准小

                sacleWidth = width;

                sacleHeigh = height;

            }else{

                //宽度比标准大 计算高度

                sacleHeigh = 400;

                sacleWidth = width / (height / targetHeight);

            }

 

            

        }

        if(widthFactor > heightFactor){

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

        }else if(widthFactor < heightFactor){

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

        }

        asize = CGSizeMake(sacleWidth, sacleHeigh);

    }

    UIGraphicsBeginImageContext(asize);

 

    UIImage *imageD = nil;

    if(newImage == nil){

        NSLog(@"scale image fail");

    } 

    [image drawInRect:CGRectMake(0,0,asize.width,asize.height)];

    UIImage* iamgeNews = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    imageD = [self imageWithJPGRepresentTationWith:iamgeNews withSacle:0.6f];

    return imageD;

    

}

// 对图片进行压缩

+ (UIImage *)imageWithJPGRepresentTationWith:(UIImage *)image withSacle:(CGFloat)sacle {

    NSData *imageData = UIImageJPEGRepresentation(image, sacle);

    NSData *imageData1 = UIImageJPEGRepresentation(image, 1.0f);

    NSData *imageData0 = UIImageJPEGRepresentation(image, 0.0f);

    UIImage *imag = nil;

//大于50k压缩,小于50k不作处理

    if(imageData.length / 1000 > 50){

        NSData *imageData2 = UIImageJPEGRepresentation(image, sacle);

        imag = [UIImage imageWithData:imageData2];

        NSLog(@"%ld",(long)imageData2.length / 1000);

    }else{

        imag = [UIImage imageWithData:imageData];

    }

    NSLog(@"%ld",(long)imageData.length / 1000);

    NSLog(@"%ld",(long)imageData1.length / 1000);

    NSLog(@"%ld",(long)imageData0.length / 1000);

    return imag;

}

 

转载于:https://www.cnblogs.com/leeFengQuan/p/5422167.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中常用的图片压缩方式有两种:质量压缩尺寸压缩。 1. 质量压缩 质量压缩是指在不改变图片大小的前提下减小图片的存储空间,即减小图片的文件大小。这种压缩方式不会改变图片的分辨率,也不会影响图片的清晰度,但是会导致一定程度的失真。在Android中,可以使用Bitmap类的compress方法进行质量压缩。 示例代码: ```java public Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 50, baos); byte[] bytes = baos.toByteArray(); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } ``` 其中,第二个参数50表示压缩质量,取值范围是0-100,数字越小,压缩后的图片质量越低。 2. 尺寸压缩 尺寸压缩是指通过改变图片的分辨率来减小图片的存储空间,即减小图片的像素数。这种压缩方式会导致图片的清晰度下降,但是不会导致失真。在Android中,可以使用Bitmap类的createScaledBitmap方法进行尺寸压缩。 示例代码: ```java public Bitmap compressImage(Bitmap image) { int width = image.getWidth(); int height = image.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(0.5f, 0.5f); // 将图片缩小一半 Bitmap compressedBitmap = Bitmap.createBitmap(image, 0, 0, width, height, matrix, true); return compressedBitmap; } ``` 其中,Matrix类表示一个3x3的矩阵,通过postScale方法可以设置图片的缩放比例。最后一个参数true表示保持缩放后的图片与原图的宽比一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值