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. 关于CSS样式的那些事_导航条菜单讲解

    最近开始忙着开自己的个人博客了,自己的前端确实是渣渣.没办法,一步步来,从慕课网上慢慢学着先. 首先带来的是一个导航栏的设计: 垂直导航栏的设计: 直接上代码: <!DOCTYPE html P ...

  2. amazeui 搜索 动态

    <!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...

  3. Openjudge-计算概论(A)-完美立方

    描述: a的立方 = b的立方 + c的立方 + d的立方为完美立方等式.例如12的立方 = 6的立方 + 8的立方 + 10的立方 .编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组 ...

  4. input type="file" 的一些问题

    file可以上传文件,但通常 情况下大家都会需要设置文件上传的格式 上传文件的格式由一个 accept 属性来控制 列如: <input type="file" id=&qu ...

  5. 快速破解ps方法

    1.首先现在ps安装包和破解包. 2.运行Block Adobe Activation,防止ADOBE激活程序启动,按操作提示即可. 3.运行Adobe CS6安装程序. 4.选择“试用”. 5.输入 ...

  6. SCU 1069 POJ 2955 Brackets

    区间DP #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  7. iOS下uiview和uiscrollview设置背景图片的源码

    1.uiscrollview 设置背景图片 // Setup the Scroll ViewUIScrollView*tempScrollView=(UIScrollView*)self.view;t ...

  8. 第十一节,编辑器软件PyCharm 5.0.3

    编辑器软件PyCharm 5.0.3 设置 主题方案 字体大小 行距 文件模板 文件编码 版本切换

  9. 使用Java7提供的Fork/Join框架

    http://blog.csdn.net/a352193394/article/details/39872923 使用Java7提供的Fork/Join框架 2014-10-07 23:55 4818 ...

  10. hdu_5791_Two(DP)

    题目链接:hdu_5791_Two 题意: 给你两串数列,问你相同的子序列有多少个,要注意,可以重复,比如1 和1 1 1 ,相同的子序列为3个 题解: 就和求最长公共子序列差不多,只不过要全部加起来 ...