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 [ ...
随机推荐
- SSL单向认证和双向认证原理
注:本文为个人学习摘录,原文地址:http://edison0663.iteye.com/blog/996526 为了便于更好的认识和理解 SSL 协议,这里着重介绍 SSL 协议的握手协议.SSL ...
- JVM基础(3)-多态性实现机制
一.方法解析 Class 文件的编译过程中不包含传统编译中的连接步骤,一切方法调用在 Class 文件里面存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址. 因此,想要使用这些符号引用 ...
- 2016NEFU集训第n+5场 A - Chinese Girls' Amusement
Description You must have heard that the Chinese culture is quite different from that of Europ ...
- chapter8_1 编译执行和错误
1.编译 前面介绍的,dofile是一个运行lua代码块的基本操作,实际上它是一个辅助函数. loadfile才真正做了核心的工作.dofile(打开文件,执行里面的代码块)和loadfile(从文件 ...
- Prime Path(BFS)
Prime Path Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- git: reset
git reset --hard:把commit撤销,意思是不仅此次commit提交的文件从本地版本库的状态重置,而且把此次commit的文件也从本地目录中删除 所以如果你执行之后发现,git sta ...
- SELinux Policy Macros
参考:http://selinuxproject.org/page/NB_RefPolicy Directory Macros macro expansion getattr_dir_perms ge ...
- JDK下载和安装
1.下载并安装JDK ,最新版本为1.8.0,官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击所 ...
- php+ajax的三级联动下拉菜单
封装一个三级联动,就可以在任何页面进行引用了 先写个页面引用一下这个js <head> <meta http-equiv="Content-Type" conte ...
- AndroidManifest.xml file missing 解决方案
如果在导入一个项目到ECLIPSE里时,不要把项目文件放到workspace里面,放到别处再导,否则可能会提示这个错误:AndroidManifest.xml file missing,顺便导入的方式 ...