本文转载至 http://blog.sina.com.cn/s/blog_6f29e81f0101tat6.html

//按比例缩放,size 是你要把图显示到 多大区域 CGSizeMake(300, 140)

-(UIImage *) imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{

UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;

CGFloat width = imageSize.width;

CGFloat height = imageSize.height;

CGFloat targetWidth = size.width;

CGFloat targetHeight = size.height;

CGFloat scaleFactor = 0.0;

CGFloat scaledWidth = targetWidth;

CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

if(CGSizeEqualToSize(imageSize, size) == NO){

CGFloat widthFactor = targetWidth / width;

CGFloat heightFactor = targetHeight / height;

if(widthFactor > heightFactor){

scaleFactor = widthFactor;

}

else{

scaleFactor = heightFactor;

}

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(size);

CGRect thumbnailRect = CGRectZero;

thumbnailRect.origin = thumbnailPoint;

thumbnailRect.size.width = scaledWidth;

thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();

if(newImage == nil){

NSLog(@"scale image fail");

}

UIGraphicsEndImageContext();

return newImage;

}

//指定宽度按比例缩放

-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{

UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;

CGFloat width = imageSize.width;

CGFloat height = imageSize.height;

CGFloat targetWidth = defineWidth;

CGFloat targetHeight = height / (width / targetWidth);

CGSize size = CGSizeMake(targetWidth, targetHeight);

CGFloat scaleFactor = 0.0;

CGFloat scaledWidth = targetWidth;

CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

if(CGSizeEqualToSize(imageSize, size) == NO){

CGFloat widthFactor = targetWidth / width;

CGFloat heightFactor = targetHeight / height;

if(widthFactor > heightFactor){

scaleFactor = widthFactor;

}

else{

scaleFactor = heightFactor;

}

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(size);

CGRect thumbnailRect = CGRectZero;

thumbnailRect.origin = thumbnailPoint;

thumbnailRect.size.width = scaledWidth;

thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();

if(newImage == nil){

NSLog(@"scale image fail");

}

UIGraphicsEndImageContext();

return newImage;

}

最新文章

  1. 工作中遇到的一个多线程下导致RCW无法释放的问题
  2. php调接口
  3. Java Native Interface 四--JNI中引用类型
  4. &quot;Fatal error: Call to undefined function: file_put_contents()&quot;
  5. MySql索引简介
  6. iptables规则组成
  7. win10 office2013激活工具
  8. Mvc4_ActionLink跟@RenderBody ,@RenderPage
  9. UVa 1607 (二分) Gates
  10. The Woman in Red Is Seen as a Threat by Other Wom
  11. 【BZOJ1901】 Zju2112 Dynamic Rankings(树套树)
  12. JQuery UI 精品UI推荐
  13. JAVA 代码生成。SimpleCaptcha
  14. R语言︱排序问题
  15. ONCOCNV软件思路分析之tumor处理
  16. MaterialCalendarDialog【Material样式的日历对话框】
  17. 收藏Dotnetbar的官方学习链接
  18. Core Animation 动画的使用:关键帧动画、基础动画、动画组
  19. ng-repeat的用法:
  20. [学习笔记]树套树 线段树套Splay

热门文章

  1. JavaScript高级 面向对象(6)--值类型与引用类型的存储特征
  2. Java中数据库连接池原理机制的详细讲解
  3. win8 X64 未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
  4. NFS根文件系统
  5. pku1204 Word Puzzles AC自动机 二维字符串矩阵8个方向找模式串的起点坐标以及方向 挺好的!
  6. [转]Hash碰撞冲突解决方法总结
  7. Applet Mode
  8. js学习笔记31----工厂方式
  9. r绘图基本
  10. fanqiang_bak