惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
A
Arctic Wolf
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
A
About on SuperTechFans
P
Palo Alto Networks Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
T
Tor Project blog
IT之家
IT之家
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
N
News and Events Feed by Topic
The Cloudflare Blog
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LangChain Blog
I
InfoQ
F
Full Disclosure
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
V
V2EX

Oragekk's Blog

OpenSpec 技术分享:用规格驱动 AI 编程 ChatGPT&Codex订阅教程 一个 waline 评论系统bug引发的思考 Jenkins 远程触发构建踩坑记 Git SSH 密钥配置 初识Rust vuepress-plugin-meting2 谷歌发布多平台应用开发神器Project IDX!PaLM 2加持 Vue常见优化手段 Vue2响应式原理解析 Dart 中的并发 Flutter 工作原理 如何利用GitHub Action提交URL到搜索引擎 提交URL到搜索引擎(百度、Bing、Google) GitHub Actions 使用介绍 素材设计 前端-Q&A 浏览器的事件循环 你不知道的 CSS 之包含块 CSS 属性计算过程 Vercel deploy忽略指定分支 评论插件 Waline 之邮件通知配置 公开API 终端究极美化iTerm2+Pure 使用Bing API提交网站URL Flutter 基础大集合 关于本站 关于本站 关于我 Markdown 展示 使用n命令管理node版本 幻灯片页 ReactNative State(状态) ReactNative开发环境配置,ES6语法介绍 ReactNative介绍 更优雅强大的终端ZSH 神经网络模型训练 YYCache优秀的缓存设计 WebViewJavascriptBridge NSError WCDB漫谈 优雅的实现TableViewCell单选 初探机器学习框架CoreML 深入理解swift中闭包的捕捉语义 ijkPlayer 集成 iOS 配置https iOS timelineLogistics iOS Cookie的配置及使用 WKWebView拦截URL WKWebView使用及自适应高度 textfield限制输入字符 评论系统从多说迁移到disqus指南 利用Runtime进行快速归档 iOS 10.3 keychain 重大更新 Cell的accessoryType属性标记单元格之后,出现的重用问题 通过UserAgent判断设备 AFNetworking A memory leak 一人一句宋词 OC 中的枚举类型 iOS程序启动原理(下) iOS程序启动原理(上) NSOperatioin TableView性能优化 Runloop Test Three ways to call Update Cocoapods 1.1.1 减小iOS-App或者静态库体积 Jekyll旧站回忆 JavaScript ES6 CommonJS,RequireJS,SeaJS 归纳笔记 Unix/Linux 扫盲笔记
iOS - Image compression algorithm
Oragekk · 2016-12-31 · via Oragekk's Blog

由于最近公司在做图片相册选择上传的功能,对于图片的压缩算法这里我借鉴了 ochina 的 ios 端 App。其中有涉及到图片压缩的算法,这里贴出来留待后用;

GACompressionPicHandle.h

    	#import <Foundation/Foundation.h>
    	#import <UIKit/UIKit.h>
    	#import <CoreGraphics/CoreGraphics.h>

    	@class GACompressionPicHandle;
    	@protocol GACompressionPicHandleDelegate <NSObject>
    	- (void)CompressionPicHandle:(GACompressionPicHandle* )handle
      			CompressionFailureInfo:(NSString* )info;
      	@end

      	#define CompressionMax_W 1224
      	#define CompressionMax_Size 300 * 1024
      	/** OSChina 后台限制上传图片的大小*/
      	#define stand_size 1024 * 800
      	#define min_compressionRatio 0.3

      	@interface GACompressionPicHandle : NSObject

      	+ (instancetype)shareCompressionPicHandle;

    @property(nonatomic,weak)id<GACompressionPicHandleDelegate> delegate;

    	/** 分辨率越小反而越占用时间 建议分辨率高的图片使用*/
    - (NSData* )scaleToSize:(CGSize)size
                   image:(UIImage* )picture;

         /** 受到宽高比例问题 越接近正方形所用的时间越小 */
    - (NSData *)imageByScalingAndCroppingForSize:(	CGSize)targetSize image:(UIImage *)image;
    	@end

GACompressionPicHandle.m

#import "GACompressionPicHandle.h"

@implementation GACompressionPicHandle

static GACompressioinPicHandle*_shareCompressionPicHandle;

+ (instancetype)shareCompressionPicHandle{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareCompressionPicHandle = 					[[GACompressionPicHandle alloc]init];
    });
    return _shareCompressionPicHandle;
}


- (NSData* )scaleToSize:(CGSize)sizeimage:(UIImage* )picture {
    CGFloat width = CGImageGetWidth(picture.CGImage);
    CGFloat height = CGImageGetHeight(picture.CGImage);
    float verticalRadio = size.height * 1.0 / height;
    float horizontalRadio = size.width * 1.0 / width;
    float radio = 1;
    if(verticalRadio > 1 && horizontalRadio > 1) {
    radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;
    }else{
    radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
    }
    width = width * radio;
    height = height * radio;

    int xPos = (size.width - width) / 2;
    int yPos = (size.height - height) / 2;

    UIGraphicsBeginImageContext(size);

    [picture drawInRect:CGRectMake(xPos, yPos, width, height)];

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    NSData* scaledImageData = UIImageJPEGRepresentation(scaledImage, 1);

    CGFloat compressionRatio = 0.9f;
    NSData* tagerImageData = scaledImageData;
    NSLog(@"tagerImageData.length : %lu",(unsigned long)tagerImageData.length);
    while (tagerImageData.length > stand_size && compressionRatio > 0) {
        if (compressionRatio < min_compressionRatio) {
            if ([_delegate respondsToSelector:@selector(CompressionPicHandle:CompressionFailureInfo:)]) {
                [_delegate CompressionPicHandle:self CompressionFailureInfo:@"图片过大"];
            }
        }
        @autoreleasepool {
            NSData* newCompressionData = UIImageJPEGRepresentation(scaledImage, compressionRatio);
            tagerImageData = newCompressionData;
        }
        compressionRatio = compressionRatio - 0.12;
        NSLog(@"tagerImageData.length : %lu , compressionRatio : %lf",tagerImageData.length,compressionRatio);
    }
    NSLog(@"compressionRatio : %lf",compressionRatio);

    return tagerImageData;
}


- (NSData *)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage *)image {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
    CGFloat widthFactor = targetWidth / width;
    CGFloat heightFactor = targetHeight / height;
    if (widthFactor > heightFactor){
    scaleFactor = widthFactor; // scale to fit height
    }else{
        scaleFactor = heightFactor; // scale to fit width
    }

    scaledWidth= width * scaleFactor;
    scaledHeight = height * scaleFactor;

    if (widthFactor > heightFactor) {
        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
    } else if (widthFactor < heightFactor){
        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    }
    }

    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
    UIGraphicsEndImageContext();

    NSData* scaledImageData = UIImageJPEGRepresentation(newImage, 1);

    CGFloat compressionRatio = 0.9f;
    NSData* tagerImageData = scaledImageData;
    NSLog(@"tagerImageData.length : %lu",(unsigned long)tagerImageData.length);
    while (tagerImageData.length > stand_size && compressionRatio > 0) {
        if (compressionRatio < min_compressionRatio) {
            if ([_delegate respondsToSelector:@selector(CompressionPicHandle:CompressionFailureInfo:)]) {
                [_delegate CompressionPicHandle:self CompressionFailureInfo:@"图片过大"];
            }
        }
        @autoreleasepool {
            NSData* newCompressionData = UIImageJPEGRepresentation(newImage, compressionRatio);
            tagerImageData = newCompressionData;
        }
        compressionRatio = compressionRatio - 0.12;
        NSLog(@"tagerImageData.length : %lu , compressionRatio : %lf",tagerImageData.length,compressionRatio);
    }
    NSLog(@"compressionRatio : %lf",compressionRatio);

    return tagerImageData;

}