UIImage扩展用代码直接改变图片大小
以下内容属于转载
在iOS中,uiimage没有用于修改大小的属性,要在代码中改变uiimage图片的大小,需要扩展UIImage类,如下:
头文件:
#import<UIKit/UIKit.h>
@interface UIImage (UIImageExtras)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize;
@end
#import"UIImage+UIImageExtras.h"
@implementation UIImage (UIImageExtras)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) ==NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage
drawInRect:thumbnailRect];
newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
return newImage ;
}
@end
写好扩展类之后,就在要修改UIImage大小的viewcontroller.h头文件空先导入你的写好的扩展类,我的是#import “UIImage+UIImageExtras.h”
这样,你就可以调用你对UIImage写的那个扩展方法了。
我调用的地方
UIImage *tabbarimage=[UIImageimageNamed:@"xxx.png"];
CGSize imagesize=CGSizeMake(50.0, 50.0);
self.tabBarItem.image=[tabbarimageimageByScalingToSize:imagesize];
下面是网上另外一个扩展UIImage类的版本,头文件我就不发了,直接上.m文件
@implementationUIImage
(Category)-
(UIImage*)transformWidth:(CGFloat)width height:(CGFloat)height
{ CGFloat
destW = width; CGFloat
destH = height; CGFloat
sourceW = width; CGFloat
sourceH = height; CGImageRef
imageRef = self.CGImage; CGContextRef
bitmap = CGBitmapContextCreate(NULL, destW, destH, CGImageGetBitsPerComponent(imageRef), 4*destW, CGImageGetColorSpace(imageRef), (kCGBitmapByteOrder32Little
| kCGImageAlphaPremultipliedFirst)); CGContextDrawImage(bitmap,
CGRectMake(0, 0, sourceW, sourceH), imageRef); CGImageRef
ref = CGBitmapContextCreateImage(bitmap); UIImage
*result = [UIImage imageWithCGImage:ref]; CGContextRelease(bitmap); CGImageRelease(ref); returnresult;}@endUIImage扩展用代码直接改变图片大小的更多相关文章
- C#改变图片大小
今天一女同事要做一个改变图片大小的功能,然后我就手写了几行代码,以后可能用得上 byte[] buffer = new byte[1]; //Byte转为Image对象 MemoryStream ms ...
- ios 改变图片大小缩放方法
http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/39089858 ...
- imagecopyresampled()改变图片大小后质量要比imagecopyresized()高。
php程序中改变图片大小的函数大多数人都想到用imagecopyresized(),不过经过测试比较发现,使用imagecopyresampled()改变的图片质量更高. 下面我们来看看两者的比较结果 ...
- JavaScript--鼠标滚动改变图片大小
鼠标滚动改变图片的大小: 原理:当鼠标滚动时改变了zoom的值: <!DOCTYPE HTML> <html> <head> <title>通过鼠标滚轮 ...
- photoshop改变图片大小,不改变像素
用画图修改了图片像素,360*440 但是图片30K,要求图片20K 打开photoshop,打开图片,点击文件--存储为web所用格式,调节品质大小到20K以下,保存即可
- JavaScript在智能手机上的应用-用手势来改变图片大小
---------------------------------- <script type="text/javascript"> //初始化函 ...
- Qt 改变图片大小
void Setting_TabProduct::changeImageSize(int width,int height,QString imgFile) { QPixmap pixmap(imgF ...
- Python之批量改变图片大小
image_pylib模块:https://github.com/huangshiyu13/image_pylib data_engine模块:https://github.com/huangshiy ...
- mui-图文列表 图片大小问题
下面是源码,不能调节图片大小 <ul class="mui-table-view"> <li class="mui-table-view-cell mu ...
随机推荐
- iOS开发-APP测试基本流程
1. UI 测试app主要核ui与实际设计的效果图是否一致:交互方面的问题建议,可以先与产品经理确认,确认通过后,才开始让开发实施更改或优化 2. 功能测试根据软件说明或用户需求验证App的各个功能实 ...
- maven 项目发布时,无法引用 修改的domain 问题
其实是在bo 的smart-score 里面引用了这个interface 所以还要把 smart -Score 重新发布一下.让这个smart 引用 新的 Player-service 中的i ...
- React+webpack开发环境的搭建
首先创建项目,确保该项目已经安装了webpack和webpack-dev-server具体安装方法请参考上章所述. 在上一章说过babel是一个javascript编辑器,在react项目中使用bab ...
- 使用curl操作InfluxDB
这里列举几个简单的示例代码,更多信息请参考InfluxDB官方文档: https://docs.influxdata.com/influxdb/v1.1/ 环境: CentOS6.5_x64Influ ...
- 用php+mysql+ajax+jquery做省市区三级联动
要求:写一个省市区(或者年月日)的三级联动,实现地区或时间的下拉选择. 实现技术:php ajax 实现:省级下拉变化时市下拉区下拉跟着变化,市级下拉变化时区下拉跟着变化. 使用chinastates ...
- webots自学笔记(五)使用物理插件ODE建立铰链
原创文章,来自"博客园,_阿龙clliu" http://www.cnblogs.com/clliu/,转载请注明原文章出处. 在一些三维制图软件或仿真软件里,都有运动副的概念,w ...
- springmvc基础学习2---简单配置文件
1:web文件 2:spring-mvc.xml配置文件
- HTML——超文本标记语言(表单及12个表单元素)
表单 格式: <form action=" " method="get/post" placehoder=" "></f ...
- 整理:20个非常有用的Java程序片段
下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...
- lvm的vg扩容
本次扩容的目的是要扩展 / 的整体容量,具体操作如下: 1.首先查看是否存在未分配的磁盘 [root@NH-Test-44 ~]# fdisk -l Disk /dev/vda: 53.7 GB, 5 ...