以下内容属于转载

在iOS中,uiimage没有用于修改大小的属性,要在代码中改变uiimage图片的大小,需要扩展UIImage类,如下:

头文件:

#import<UIKit/UIKit.h>

@interface UIImage (UIImageExtras)

- (UIImage *)imageByScalingToSize:(CGSize)targetSize;

@end

.m文件

#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;
}
@end

UIImage扩展用代码直接改变图片大小的更多相关文章

  1. C#改变图片大小

    今天一女同事要做一个改变图片大小的功能,然后我就手写了几行代码,以后可能用得上 byte[] buffer = new byte[1]; //Byte转为Image对象 MemoryStream ms ...

  2. ios 改变图片大小缩放方法

    http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/39089858 ...

  3. imagecopyresampled()改变图片大小后质量要比imagecopyresized()高。

    php程序中改变图片大小的函数大多数人都想到用imagecopyresized(),不过经过测试比较发现,使用imagecopyresampled()改变的图片质量更高. 下面我们来看看两者的比较结果 ...

  4. JavaScript--鼠标滚动改变图片大小

    鼠标滚动改变图片的大小: 原理:当鼠标滚动时改变了zoom的值: <!DOCTYPE HTML> <html> <head> <title>通过鼠标滚轮 ...

  5. photoshop改变图片大小,不改变像素

    用画图修改了图片像素,360*440 但是图片30K,要求图片20K 打开photoshop,打开图片,点击文件--存储为web所用格式,调节品质大小到20K以下,保存即可

  6. JavaScript在智能手机上的应用-用手势来改变图片大小

    ---------------------------------- <script type="text/javascript">            //初始化函 ...

  7. Qt 改变图片大小

    void Setting_TabProduct::changeImageSize(int width,int height,QString imgFile) { QPixmap pixmap(imgF ...

  8. Python之批量改变图片大小

    image_pylib模块:https://github.com/huangshiyu13/image_pylib data_engine模块:https://github.com/huangshiy ...

  9. mui-图文列表 图片大小问题

    下面是源码,不能调节图片大小 <ul class="mui-table-view"> <li class="mui-table-view-cell mu ...

随机推荐

  1. vue1升级到vue2的问题

    router 不能用map方法了,需要改router的结构改为 routers= [ { // 当没有匹配路由时默认返回的首页        path:'/index',        compone ...

  2. 信号处理——傅里叶变换(FT-DTFT-DFT)

    作者:桂. 时间:2017-01-17  23:41:13 链接:http://www.cnblogs.com/xingshansi/articles/6294111.html 声明:转载请注明出处, ...

  3. 2017-3-9 SQL server 数据库

    数据库的定义:数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,简单说数据库是一些存储在硬盘上的数据文件,随着信息技术和市场的发展,数据管理不再仅仅是存储和管理数据,而转变成用户所 ...

  4. ThinkPhp框架的数据库操作(查询)

    TP框架有一套自己的数据库操作的代码,包括数据库的增.删.改.查.本文主要讲解TP框架的数据库查询操作. 找到入口文件的控制器: 我这里的入口文件是Show文件夹下的控制器. 打开Login控制器. ...

  5. c++ string 对象操作

    字符串转换大小写如下: #include "stdafx.h" #include <iostream> #include <string> using na ...

  6. C#传递委托给C或C++库报错__对XXX类型的已垃圾回收委托进行了回调

    出现的原因: 因为你传给C或C++的委托是局部的.可能传过去之后就被垃圾回收了,再次调用就会异常. 想办法做成全局的就好 public void Play(string url) { _bassStr ...

  7. Java程序员应当知道的10个面向对象设计原则

    面向对象设计原则是OOPS编程的核心, 但我见过的大多数Java程序员热心于像Singleton (单例) . Decorator(装饰器).Observer(观察者) 等设计模式,而没有把足够多的注 ...

  8. eclipse中代码格式化组合键失效了

    因为最近在整理java笔记,发现代码格式化的组合键失效了,但其他的组合键是好着的,设置里面找了也是对着的. 最后是在输入法的设置里面发现了它,原来是快捷键冲突了.取消输入法里面 的快捷键就好了,或者改 ...

  9. (转)centos6.5安装VNC

    在Linux下用VNC远程桌面是个很不错的玩意.但在CentOS中默认没有安装VNC的.可以用下面语句查询,如果出现下面情况说明没有安装vnc #rpm -q tigervnc tigervnc-se ...

  10. 查看apache,mysql,nginx,php的编译参数

    查看nginx编译参数:/usr/local/nginx/sbin/nginx -V 查看apache编译参数:cat /usr/local/apache2/build/config.nice 查看m ...