UIImage+Scale
| @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的更多相关文章
- UIImage 相关操作
修改UIImage大小 修改UISlider的最大值和最小值图片的时候,发现需要修改图片的大小,否则会导致UISlider变形.目前苹果还不支持直接修改UIImage类的大小,只能修改UIImageV ...
- iOS项目开发中的知识点与问题收集整理①(Part 一)
前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...
- ios开发学习笔记(这里一定有你想要的东西,全部免费)
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS 开发笔记
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用) 2,NSDate使用 3,UTTabviewCell 未 ...
- iOS项目开发知识点
前言部分 注:本文并非绝对原创 大部分内容摘自 http://blog.csdn.net/hengshujiyi/article/details/20943045 文中有些方法可能已过时并不适用于现在 ...
- UI:字典的两种取值的区别
字典的两种取值的区别 (objectForKey: 和 valueForKey )参考 一般来说 key 可以是任意字符串组合,如果 key 不是以 @ 符号开头,这时候 valueForKey: 等 ...
- UI:UITableView 编辑、cell重用机制
tableView编辑.tableView移动.UITableViewController tableView的编辑:cell的添加.删除. 使⽤场景: 删除⼀个下载好的视频,删除联系⼈: 插⼊⼀条新 ...
- UITableview自定义accessory按钮和ImageView大小一致
if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseI ...
- 【转】IOS开发小技巧
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
随机推荐
- JVM基础(3)-多态性实现机制
一.方法解析 Class 文件的编译过程中不包含传统编译中的连接步骤,一切方法调用在 Class 文件里面存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址. 因此,想要使用这些符号引用 ...
- 使用GNU/Linux播放电视节目
目前,生活中很多事情都可以在电脑前完成,读书.写程序.听音乐.看视频等.如果也可以在电脑上收看有线电视节目的话,那就更好了.为此,我购买了圆刚视频采集卡AverMedia C725B.如下图所示. 官 ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 3259 Wormholes
SPFA求负环 模板题 记得每组处理之前clear vector /* *********************************************** Author :Sun Yuef ...
- amazeui tab 监听当前选项
$('#contenttab').find('a').on('opened.tabs.amui', function(e) { if(e.target.pathname.indexOf("[ ...
- RPM安装gcc gcc-c++扩展
rpm -ivh cpp--.el5.i386.rpm 回车 rpm -ivh kernel-headers--.el5.i386.rpm 回车 rpm -ivh glibc-headers-.i38 ...
- sync命令
sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...
- How can I create an Asynchronous function in Javascript?
哈哈:)我的codepen 的代码笔记是:http://codepen.io/shinewaker/pen/eBwPxJ --------------------------------------- ...
- Python基础篇-day3
主要内容:字典 集合 文件处理 字符编码 1.字典dict简介dict就是key value值,索引有意义,数据无序 key定义规则:a:不可变--数字.字符串.元组(可变--列表.字典)b:不能重复 ...
- Position & anchorPoint 深入
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...
- Flask -- 静态文件 和 模板渲染
静态文件 一般用于存放图片,样式文件(css, js等) 保存位置:包中或者文件所在目录创建一个 static 目录 访问:在应用中使用 /static/...即可访问 , 更好的方式是使用url_f ...