图片处理

Posted iWillW

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片处理相关的知识,希望对你有一定的参考价值。

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

 

以上是关于图片处理的主要内容,如果未能解决你的问题,请参考以下文章

如何把视频片段做成动态图片

手机safari图片上传竖变横处理

CardView 不在披萨片段中显示图片

Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题

Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题

Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段