+(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=;
//输出图片参数
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[]; /* 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();
}
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 * ; /* 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[] = & image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, );
} /* 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 format
CGImageRef 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 * );
unsigned char *outPixel= malloc(sizeof(unsigned char) * height * width * ); int bytesPerPixel = ;
int bytesPerRow = bytesPerPixel * width;
int bitsPerComponent = ;
CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGContextDrawImage(context, CGRectMake(, , width, height), imageRef);
CGContextRelease(context); for (int y=; y<height; y++)
{
for (int x=; x<width; x++)
{
int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
int outIndex=(*width*y)+x*;
outPixel[outIndex+]= m_PixelBuf[byteIndex+];
outPixel[outIndex+]= m_PixelBuf[byteIndex+];
outPixel[outIndex+]= m_PixelBuf[byteIndex+];
}
} CGColorSpaceRelease(colorSpace);
free(m_PixelBuf);
free(outPixel);
return outPixel; }

使用libjpeg.framework压缩UIImage的更多相关文章

  1. UIImage 和 iOS 图片压缩UIImage / UIImageVIew

    UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...

  2. iOS 关于监听手机截图,UIView生成UIImage, UIImage裁剪与压缩的总结

    一.  关于监听手机截图 1. 背景: 发现商品的售价页总是被人转发截图,为了方便用户添加截图分享的小功能 首先要注册用户截屏操作的通知 - (void)viewDidLoad { [super vi ...

  3. iOS 图片大小压缩 图片尺寸处理

    图片的压缩其实是俩概念,1.是 “压” 文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降,2.是 “缩” 文件的尺寸变小,也就是像素数减少.长宽尺寸变小,文件体积同样会减小. 这个 UII ...

  4. IOS_画图 图片等比压缩 IOS_UIImage

    - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{ // 创建一个bitmap的context // 并把它设置成为当前正在使用的co ...

  5. iOS 图片按比例压缩,指定大小压缩

    使用系统方法UIImageJPEGRepresentation(UIimage *image,CGFloat quality)进行图片质量压缩,暂且叫参数quality为压缩比吧,取值范围为0-1. ...

  6. Android 中图片压缩分析(上)

    作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...

  7. UIImage常用封装

    根据颜色返回图片,根据str返回颜色,压缩UIImage不大于300k .h代码: #import <Foundation/Foundation.h> @interface ImageSe ...

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

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

  9. iOS 图片剪切和压缩的几个方法

    // 图片剪切 - (UIImage*)clipImageWithImage:(UIImage*)image inRect:(CGRect)rect {    CGImageRef imageRef ...

随机推荐

  1. postgresql数据库备份和恢复

    PostgreSQL自带一个客户端pgAdmin,里面有个备份,恢复选项,也能对数据库进行备份 恢复(还原),但最近发现数据库慢慢庞大的时候,经常出错,备份的文件过程中出错的几率那是相当大,手动调节灰 ...

  2. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  3. swift 命令

    http://blog.chinaunix.net/uid-15063109-id-5144658.html http://www.cnblogs.com/fczjuever/p/3224022.ht ...

  4. Dw CS 破解

    据说,CS5的破解也可以用CS6的破解方法,不过可能本人太菜,有所失误,总是不成功,安装成功后,打开总是提示 : 我们无法开始您的Adobe Dreamweaver cs5 subscription ...

  5. [NHibernate]查看NHibernate生成的SQL语句

    最近接触到一个用Spring.Net结合NHIbernate的项目,第一次使用,有很多配置,数据操作一旦出问题,很难找到原因,那么如何查看NHibernate发送给数据库的SQL语句呢? 当然我们可以 ...

  6. 無間道III 終極無間

    凭良心说,它绝对算是诚意之作,而非急功近利或者说抢市.因为导演尤其是编剧都用了心,为了和第一二集融合而在细节处理上做足了文章,麦兆辉也实在够天才. 关于时间问题,本片不是完全杂乱无章,只不过是前后两段 ...

  7. OSG消息机制之消息分析

    OSG消息接收在头文件有各种事件的相关参数

  8. webpack 打包一个简单react组件

    安装Webpack,并加载一个简单的React组件 全局的npm模块安装: npm install -g webpack 安装jsx-loader npm install --save-dev jsx ...

  9. LeetCode刷刷记录

    一遍考研,一遍还是要刷刷题.感觉自己的时间安排的不是很好,还是要抓紧自己的日常时间,当然,也要练练刷题的手感. 1.第一题就两重循环找到索引就OK,因为是无序的,所以就不能用二分来查找,题目中每个数的 ...

  10. unity 改变场景

    public class GameManager : MonoBehaviour { public void OnStartGame(int sceneName) { SceneManager.Loa ...