码迷,mamicode.com
首页 > 其他好文 > 详细

图片处理

时间:2016-09-22 15:50:41      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

+ (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;
}

 

图片处理

标签:

原文地址:http://www.cnblogs.com/dlwj/p/5896574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!