zoukankan      html  css  js  c++  java
  • 图片处理

    + (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize
    {
        //先调整分辨率
        CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
        
        CGFloat tempHeight = newSize.height / 1024;
        CGFloat tempWidth = newSize.width / 1024;
        
        if (tempWidth > 1.0 && tempWidth > tempHeight) {
            newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
        }
        else if (tempHeight > 1.0 && tempWidth < tempHeight){
            newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
        }
        
        UIGraphicsBeginImageContext(newSize);
        [source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        //调整大小
        NSData * imageData = UIImageJPEGRepresentation(newImage,1.0);
        NSUInteger sizeOrigin = [imageData length];
        NSUInteger sizeOriginKB = sizeOrigin / 1024;
        
        if (sizeOriginKB > maxSize) {
            
            NSUInteger testKB = sizeOriginKB;
            UIImage * testIamge = newImage;
            
            if (testKB > 4000) {
                imageData = UIImageJPEGRepresentation(newImage,0.10);
                testKB = [imageData length] / 1024;
                testIamge = [UIImage imageWithData:imageData];
            }
            if (testKB > 3000) {
                imageData = UIImageJPEGRepresentation(testIamge,0.20);
                testKB = [imageData length] / 1024;
                testIamge = [UIImage imageWithData:imageData];
            }
            if (testKB > 2000) {
                imageData = UIImageJPEGRepresentation(testIamge,0.30);
                testKB = [imageData length] / 1024;
                testIamge = [UIImage imageWithData:imageData];
            }
            if (testKB > 1000) {
                imageData = UIImageJPEGRepresentation(testIamge,0.40);
                testKB = [imageData length] / 1024;
                testIamge = [UIImage imageWithData:imageData];
            }
            if (testKB > 500) {
                imageData = UIImageJPEGRepresentation(testIamge,0.50);
                testKB = [imageData length] / 1024;
                testIamge = [UIImage imageWithData:imageData];
            }
            if (testKB > 300) {
                imageData = UIImageJPEGRepresentation(testIamge,0.50);
                testKB = [imageData length] / 1024;
                testIamge = [UIImage imageWithData:imageData];
            }
            if (testKB > maxSize) {
                imageData = UIImageJPEGRepresentation(testIamge,0.30);
                testKB = [imageData length] / 1024;
                testIamge = [UIImage imageWithData:imageData];
            }
            return imageData;
        }
        
        return imageData;
    }
  • 相关阅读:
    前端学习的几个网站
    程序员怎么写出一份漂亮的简历
    程序员斗图专用表情包
    2018年国内就业薪资高的7大编程语言排行
    微信小程序初步运营方案
    「干货」从菜鸟到大神,前端学习书籍推荐
    数据分析概述和理论基础
    十大厂商为什么要联合推出“快应用”对标小程序?
    数据分析的过程
    H5混合开发二维码扫描以及调用本地摄像头
  • 原文地址:https://www.cnblogs.com/dlwj/p/5896574.html
Copyright © 2011-2022 走看看