Scale a UIImage to any given rect keeping the aspect ratio
  @implementation UIImage (scale)
   
  /**
  * Scales an image to fit within a bounds with a size governed by
  * the passed size. Also keeps the aspect ratio.
  *
  * Switch MIN to MAX for aspect fill instead of fit.
  *
  * @param newSize the size of the bounds the image must fit within.
  * @return a new scaled image.
  */
  - (UIImage *)scaleImageToSize:(CGSize)newSize {
   
  CGRect scaledImageRect = CGRectZero;
   
  CGFloat aspectWidth = newSize.width / self.size.width;
  CGFloat aspectHeight = newSize.height / self.size.height;
  CGFloat aspectRatio = MIN ( aspectWidth, aspectHeight );
   
  scaledImageRect.size.width = self.size.width * aspectRatio;
  scaledImageRect.size.height = self.size.height * aspectRatio;
  scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0f;
  scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0f;
   
  UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 );
  [self drawInRect:scaledImageRect];
  UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
   
  return scaledImage;
   
  }
   
  @end

 

In Swift:

/*
Image Resizing Techniques: http://bit.ly/1Hv0T6i
*/
class func scaleUIImageToSize(let image: UIImage, let size: CGSize) -> UIImage {
let hasAlpha = false
let scale: CGFloat = 0.0 // Automatically use scale factor of main screen UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
image.drawInRect(CGRect(origin: CGPointZero, size: size)) let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext() return scaledImage
}

 

In Rubymotion

class UIImage

  # Scales an image to fit within a bounds with a size governed by
# the passed size. Also keeps the aspect ratio.
#
# newSize - the CGSize of the bounds the image must fit within.
# aspect - A Symbol stating the aspect mode (defaults: :min)
#
# Returns a new scaled UIImage
def scaleImageToSize(newSize, aspect = :fit)
scaledImageRect = CGRectZero aspectRules = { :fill => :max } # else :min aspectWidth = Rational(newSize.width, size.width)
aspectHeight = Rational(newSize.height, size.height) aspectRatio = [aspectWidth, aspectHeight].send(aspectRules[aspect] || :min) scaledImageRect.size = (size.width * aspectRatio).round
scaledImageRect.size.height = (size.height * aspectRatio).round scaledImageRect.origin.x = Rational(newSize.width - scaledImageRect.size.width, 2.0).round
scaledImageRect.origin.y = Rational(newSize.height - scaledImageRect.size.height, 2.0).round UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
drawInRect(scaledImageRect)
scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
scaledImage
end end

 

In Swift 2 (with keeping aspect ratio)

func imageWithSize(size:CGSize) -> UIImage
{
var scaledImageRect = CGRect.zero; let aspectWidth:CGFloat = size.width / self.size.width;
let aspectHeight:CGFloat = size.height / self.size.height;
let aspectRatio:CGFloat = min(aspectWidth, aspectHeight); scaledImageRect.size.width = self.size.width * aspectRatio;
scaledImageRect.size.height = self.size.height * aspectRatio;
scaledImageRect.origin.x = (size.width - scaledImageRect.size.width) / 2.0;
scaledImageRect.origin.y = (size.height - scaledImageRect.size.height) / 2.0; UIGraphicsBeginImageContextWithOptions(size, false, 0); self.drawInRect(scaledImageRect); let scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return scaledImage;
}

If you want "aspect fill", instead of "aspect fit", change function min to max.

UIImage+Scale的更多相关文章

  1. UIImage 相关操作

    修改UIImage大小 修改UISlider的最大值和最小值图片的时候,发现需要修改图片的大小,否则会导致UISlider变形.目前苹果还不支持直接修改UIImage类的大小,只能修改UIImageV ...

  2. iOS项目开发中的知识点与问题收集整理①(Part 一)

    前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...

  3. ios开发学习笔记(这里一定有你想要的东西,全部免费)

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  4. iOS 开发笔记

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用) 2,NSDate使用 3,UTTabviewCell 未 ...

  5. iOS项目开发知识点

    前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...

  6. UI:字典的两种取值的区别

    字典的两种取值的区别 (objectForKey: 和 valueForKey )参考 一般来说 key 可以是任意字符串组合,如果 key 不是以 @ 符号开头,这时候 valueForKey: 等 ...

  7. UI:UITableView 编辑、cell重用机制

    tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...

  8. UITableview自定义accessory按钮和ImageView大小一致

    if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseI ...

  9. 【转】IOS开发小技巧

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

随机推荐

  1. 大数加法(STL list)

    #include<iostream> #include<list> #include<string> using namespace std; int main() ...

  2. JS学习之路,之弹性运动框架

    弹性运动:顾名思义,就如同物理中的加速减速运动,当开始时速度过大,到达终点时,速度不会立刻停下,而是再前进一段距离,而后再向相反方向运动,如此往复. var timer=null; var speed ...

  3. chapter9_3 协同程序实现迭代器

    将循环迭代器视为"生产者-消费者"模式的一种特例:迭代器产生的数据供循环体消费. 因此,用协同程序写迭代器就理所当然了.因为协同程序可以一改传统调用者与被调用者之间的关系. 有了这 ...

  4. Android应用测试性能的工具Emmagee,导出文件格式问题分析

    原文引用自:http://www.open-open.com/lib/view/open1367026451078.html Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资 ...

  5. Weblogic的集群

    <收藏自http://www.cnblogs.com/HondaHsu/p/4267972.html> 一.Weblogic的集群 还记得我们在第五天教程中讲到的关于Tomcat的集群吗? ...

  6. evernote

    evernote hebinn@163.com How to get Note Link Right Click Note & Copy Note Link Open Note : Note- ...

  7. Python Cookbook - 1 - 数据结构和算法

    1 序列解压:通过*来通配 *a, b = somelist,   first, *mid, last = somelist,  a, *b = somelist 2 使用双向队列: from col ...

  8. 还原openstack配置文件的方法

    cp -a /etc/neutron/neutron.conf /etc/neutron/neutron.conf.bakcp -a /etc/neutron/plugins/ml2/ml2_conf ...

  9. .net core 时间戳转换

    /// <summary> /// 时间戳和日期的转化 /// </summary> public class DateTimeUtility { /// <summar ...

  10. hdu_2224_The shortest path(dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2224 题意:双调欧几里德旅行商经典问题,找一条最短回路使得该路经过所有的点 题解:dp[i][j]=d ...