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 ...
随机推荐
- Matlab立体标定mat转换成Opencv的CvMat
最近在做基于双目视觉的三维重建.比较opencv和matlab工具箱的立体标定结果精度时,发现貌似如果手工选取角点不那么离谱的话,matlab标定结果精度更高也更鲁棒.就想先用matlab标定好相机, ...
- ASP.NET MVC 使用Uploadify实现多文件异步无刷新上传
软件技术开发,合作请联系QQ:858-048-581 这里我通过使用uploadify组件来实现异步无刷新多文件上传功能. 1.首先下载组件包uploadify,我这里使用的版本是3.1 2.下载后解 ...
- Material Design学习-----FloatingActionButton
FloatingActionButton是悬浮操作按钮,它继承自imageview,所以说它具备有imageview所有的方法和属性.与其他按钮不同的是,FloatingActionButton默认就 ...
- FaceNet---深度学习与人脸识别的二次结合
今天我给大家带来一篇来自谷歌的文章,众所周知,谷歌是全世界最有情怀,最讲究技术的公司,比我们天朝的莆田广告商良心多了.还有就是前段时间的最强大脑,莆田广告商的那个小机器,也就忽悠忽悠行外人了,懂的人深 ...
- 项目架构开发:数据访问层之Query
接上文 项目架构开发:数据访问层之Repository 上一章我们讲了IRepository接口,这张我们来讲IQuery 根据字面意思就可以知道,这次主要讲数据查询,上一章我们只针对单表做了查询的操 ...
- 使用ocupload和POI一键上传Excel并解析导入数据库
使用的工具如下: JQuery ocupload jquery.ocupload-1.1.2.js Apache POI poi-3.9.jar 如果是Maven项目添加依赖如下: <depe ...
- Robot Framework自动化测试环境部署
文档版本:v1.0 作者:令狐冲 如有问题请发邮件到:1146009864@qq.com 使用Robot Framework框架(以下简称RF)来做自动化测试. 模块化设计 1.所需环境一览表 软件 ...
- java基础:学生管理系统
package com.lovo.manager; import java.util.Scanner; /** * 学生管理 * * @author 向往的生活 * */public class St ...
- 3399: [Usaco2009 Mar]Sand Castle城堡
3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 37 Solved: 32[Sub ...
- 3390: [Usaco2004 Dec]Bad Cowtractors牛的报复
3390: [Usaco2004 Dec]Bad Cowtractors牛的报复 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 69 Solved: ...