+(void)writeFile:(NSString *)filePath withQuality:(int)quality
{//初始化图片参数UIImage *image=[UIImage imageNamed:@"testimg.bmp"];JSAMPLE *image_buffer = (JSAMPLE *)[self RGBDataForImage:image];int image_width = image.size.width;int image_height= image.size.height;int image_components=3;//输出图片参数const char * filename=[filePath UTF8String];/* This struct contains the JPEG compression parameters and pointers to* working space (which is allocated as needed by the JPEG library).* It is possible to have several such structures, representing multiple* compression/decompression processes, in existence at once.  We refer* to any one struct (and its associated working data) as a "JPEG object".*/struct jpeg_compress_struct cinfo;/* This struct represents a JPEG error handler.  It is declared separately* because applications often want to supply a specialized error handler* (see the second half of this file for an example).  But here we just* take the easy way out and use the standard error handler, which will* print a message on stderr and call exit() if compression fails.* Note that this struct must live as long as the main JPEG parameter* struct, to avoid dangling-pointer problems.*/struct jpeg_error_mgr jerr;/* More stuff */FILE * outfile;        /* target file */JSAMPROW row_pointer[1];    /* pointer to JSAMPLE row[s] */int row_stride;        /* physical row width in image buffer *//* Step 1: allocate and initialize JPEG compression object *//* We have to set up the error handler first, in case the initialization* step fails.  (Unlikely, but it could happen if you are out of memory.)* This routine fills in the contents of struct jerr, and returns jerr's* address which we place into the link field in cinfo.*/cinfo.err = jpeg_std_error(&jerr);/* Now we can initialize the JPEG compression object. */jpeg_create_compress(&cinfo);/* Step 2: specify data destination (eg, a file) *//* Note: steps 2 and 3 can be done in either order. *//* Here we use the library-supplied code to send compressed data to a* stdio stream.  You can also write your own code to do something else.* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that* requires it in order to write binary files.*/if ((outfile = fopen(filename, "wb")) == NULL) {fprintf(stderr, "can't open %s\n", filename);exit(1);}jpeg_stdio_dest(&cinfo, outfile);/* Step 3: set parameters for compression *//* First we supply a description of the input image.* Four fields of the cinfo struct must be filled in:*/cinfo.image_width = image_width;     /* image width and height, in pixels */cinfo.image_height = image_height;cinfo.input_components =image_components;        /* # of color components per pixel */cinfo.in_color_space = JCS_RGB;     /* colorspace of input image *//* Now use the library's routine to set default compression parameters.* (You must set at least cinfo.in_color_space before calling this,* since the defaults depend on the source color space.)*/jpeg_set_defaults(&cinfo);/* Now you can set any non-default parameters you wish to.* Here we just illustrate the use of quality (quantization table) scaling:*/jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);/* Step 4: Start compressor *//* TRUE ensures that we will write a complete interchange-JPEG file.* Pass TRUE unless you are very sure of what you're doing.*/jpeg_start_compress(&cinfo, TRUE);/* Step 5: while (scan lines remain to be written) *//*           jpeg_write_scanlines(...); *//* Here we use the library's state variable cinfo.next_scanline as the* loop counter, so that we don't have to keep track ourselves.* To keep things simple, we pass one scanline per call; you can pass* more if you wish, though.*/row_stride = image_width * 3;    /* JSAMPLEs per row in image_buffer */while (cinfo.next_scanline < cinfo.image_height) {/* jpeg_write_scanlines expects an array of pointers to scanlines.* Here the array is only one element long, but you could pass* more than one scanline at a time if that's more convenient.*/row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);}/* Step 6: Finish compression */jpeg_finish_compress(&cinfo);/* After finish_compress, we can close the output file. */fclose(outfile);/* Step 7: release JPEG compression object *//* This is an important step since it will release a good deal of memory. */jpeg_destroy_compress(&cinfo);
}

+(unsigned char *)RGBDataForImage:(UIImage *)image
{// Create a pixel buffer in an easy to use formatCGImageRef imageRef = [image CGImage];int width = (int)CGImageGetWidth(imageRef);int height = (int)CGImageGetHeight(imageRef);CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();unsigned char *m_PixelBuf = malloc(sizeof(unsigned char) * height * width * 4);unsigned char *outPixel= malloc(sizeof(unsigned char) * height * width * 3);int bytesPerPixel = 4;int bytesPerRow = bytesPerPixel * width;int bitsPerComponent = 8;CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height,bitsPerComponent, bytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);CGContextRelease(context);for (int y=0; y<height; y++){for (int x=0; x<width; x++){int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;int outIndex=(3*width*y)+x*3;outPixel[outIndex+0]= m_PixelBuf[byteIndex+0];outPixel[outIndex+1]= m_PixelBuf[byteIndex+1];outPixel[outIndex+2]= m_PixelBuf[byteIndex+2];}}CGColorSpaceRelease(colorSpace);free(m_PixelBuf);free(outPixel);return outPixel;}

转载于:https://www.cnblogs.com/bandy/p/4732356.html

使用libjpeg.framework压缩UIImage相关推荐

  1. Android LibJpeg图片压缩

    Android的图片压缩 Android的图片压缩的几种方式:质量压缩,尺寸压缩,采样率压缩,通过NDK调用libjpeg库进行压缩! 质量压缩 通过设置bitmap options属性,降低图片的质 ...

  2. iOS swift5 图片压缩 UIImage

    参考博客:swift 图片压缩 // // UIImage_extension.swift // F // // Created by mac on 2022/3/30. //enum ImageCo ...

  3. Android压缩图片和libjpeg库

    前言 Fjpeg使用 Fjpeg 注意 如何使用 如何压缩图片只改变在硬盘的存储大小 如何改变图片分辨率让其Bitmap对象可以加载到内存中 关于重载版本 开始学习之旅 补充知识的结论 修改图片分辨率 ...

  4. 使用libjpeg进行图片压缩

    简介 由于工作原因,boss下达的任务就大概说了对图片进行压缩寻找比较合理的方式,还举了一个项目中的坑,就是系统原生的Bitmap.compress设置质量参数为100生成图片会变大的坑.所以我打算用 ...

  5. 热更新--动态加载framework

    1.准备工作:先自己封装一个framework:http://www.cnblogs.com/sunjianfei/p/5781863.html 2.把封装好的framework压缩成zip,放到本地 ...

  6. IOS 图片上传处理 图片压缩 图片处理

    提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用.在这里,我们需要过UIImagePickerController类来和用户交互. 使用UIImagePickerContr ...

  7. bmp转jpg(使用libjpeg)

    jpg压缩原理可以参考这篇文章http://hi.baidu.com/tiandsp/item/f5a2dcde6ef1405bd73aae41,我很早以前转的一篇文章. 没有使用libjpeg的压缩 ...

  8. Android图片编码机制深度解析(Bitmap,Skia,libJpeg)

    问题 工作中遇到了Android中有关图片压缩保存的问题,发现这个问题还挺深,而且网上资料比较有限,因此自己深入研究了一下,算是把这个问题自顶至下全部搞懂了,在此记录. 相关的几个问题如下: 1.An ...

  9. Android-JNI开发系列《十》实践利用libjpeg-turbo完美压缩图片不失真

    人间观察 步入社会后,你会发现,老人说的话都是对的. 前面讲了些Android的jni知识和bitmap的实践,接下来几篇应该都是Android中jni的一些实践.这篇我们对Android中图片在jn ...

最新文章

  1. JVM - 深入剖析字符串常量池
  2. GDCM:将PDF文件转换为DICOM / PDF文件的测试程序
  3. vscode中go插件配置
  4. 个人博客 | 网站部署终极操作:一行命令搞定!
  5. 利用python爬虫(案例5)--X刺代理的小IP们
  6. 云南计算机专业知识真题,2014年云南省事业单位考试专计算机专业知识模拟真题.doc...
  7. 一次性搞懂JavaScript正则表达式之语法
  8. 二元一次函数最值问题_初二上学期,一次函数方案设计最值问题,两类题目解题思路不一样...
  9. php_memcahed telnet远程操作方法
  10. Linux下chkconfig命令介绍
  11. java 类加载器卸载,【深入明白Java虚拟机 】类加载器的命名空间以及类的卸载...
  12. bug-Skipping optimization due to error while loading function libraries: Invalid argument: Functions
  13. 自己写的一个分享按钮的插件(可扩展,内附开发制作流程)
  14. eclipse上安装并配置tomcat
  15. 华为大数据解决方案(PPT)
  16. Java实现通过证书访问Https请求
  17. java String工具类/字符串工具类 StringUtil
  18. Excel VBA中的If,Select循环语句
  19. 【FPGA算法加速】运行PYNQ,对应FPGA芯片版本:赛灵思黑金AX7020
  20. 移动设备网页中快速响应单击动作

热门文章

  1. spring实战六之使用基于java配置的Spring
  2. 求解N个值中最大的k个数,N远大于k
  3. 乘法器之五(混和式乘法器(Hybrid multiplication))
  4. 理解ASP.NET MVC Framework Action Filters(翻的)
  5. 《Thinking in UML》读书笔记之一
  6. Perl中state()和localtime()函数
  7. git查找两个分支的共同节点
  8. leetcode算法题--从先序遍历还原二叉树
  9. javascript写贪吃蛇
  10. Python之并发编程