本文转载至 http://blog.csdn.net/cuiweijie3/article/details/9514293

转自 http://www.tedz.me/ios/uiimage-crop-resize-image

@interface UIImage(UIImageScale)
-(UIImage*)getSubImage:(CGRect)rect;
-(UIImage*)scaleToSize:(CGSize)size;
@end
 
@implementation UIImage(UIImageScale)
 
//截取部分图像
-(UIImage*)getSubImage:(CGRect)rect
{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
 
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
 
    return smallImage;
}
 
//等比例缩放
-(UIImage*)scaleToSize:(CGSize)size
{
    CGFloat width = CGImageGetWidth(self.CGImage);
    CGFloat height = CGImageGetHeight(self.CGImage);
 
    float verticalRadio = size.height*1.0/height;
    float horizontalRadio = size.width*1.0/width;
 
    float radio = 1;
    if(verticalRadio>1 && horizontalRadio>1)
    {
        radio = verticalRadio > horizontalRadio ? horizontalRadio : verticalRadio;
    }
    else
    {
        radio = verticalRadio < horizontalRadio ? verticalRadio : horizontalRadio;
    }
 
    width = width*radio;
    height = height*radio;
 
    int xPos = (size.width - width)/2;
    int yPos = (size.height-height)/2;
 
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);  
 
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(xPos, yPos, width, height)];  
 
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
 
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();  
 
    // 返回新的改变大小后的图片
    return scaledImage;
}
@end
 
然后在下面方法里面调用就可以了!
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURLURLWithString:@"http://img21.mtime.cn/mg/2011/09/24/112524.53149978.jpg"]] ];
 
    // 裁剪图片
    //image = [image getSubImage:CGRectMake(10, 10, 70, 80)];
 
    //等比列缩放
 
    image = [image scaleToSize:CGSizeMake(200, 300)];
 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
 
    [self.window addSubview:imageView];
 
    NSLog(@"image.size:%@",NSStringFromCGSize(CGSizeMake(imageView.image.size.width, imageView.image.size.height)));   //打印获取的网络图片的宽度和高度
 
    [self.window makeKeyAndVisible];
    return YES;
}
 
//--------------截取部分图片到指定位置-------------------------
 
图片(UIImage*) img
要截取的起始坐标sx:(int) sx1 sy:(int)sy1
要截取的长度和宽度sw:(int) sw1 sh:(int) sh1
最终要显示的坐标desx:(int) desx1 desy:(int)desy1
 
-(UIImage*)objectiveDrawRegion:(UIImage*) img sx:(int) sx1 sy:(int)sy1 sw:(int) sw1 sh:(int) sh1 desx:(int) desx1 desy:(int)desy1{
[self saveImage:img name:@"objectiveDrawRegion1.png"];
 
//创建图片缓冲
void *imageDataRegion=malloc(screenWidth*screenHeight*32);
CGColorSpaceRef iColorSpaceRegion=CGColorSpaceCreateDeviceRGB();
    CGContextRefiDeviceRegion=CGBitmapContextCreate(imageDataRegion,screenWidth,screenHeight,8,4*screenWidth,iColorSpaceRegion,kCGImageAlphaPremultipliedLast);
 
//剪切区域
    CGRect clipRegion=CGRectMake(sx1,sy1,sw1,sh1);
    CGContextClipToRect(iDeviceRegion, clipRegion);
 
    CGFloat widthf=img.size.width;
    CGFloat heightf=img.size.height;
 
CGRect  cg=CGRectMake(0.0, 0.0, widthf, heightf);
//画底图
    CGContextDrawImage(iDeviceRegion,cg, img.CGImage);
 
//将缓冲形成图片
    CGImageRef ioffRegion=CGBitmapContextCreateImage(iDeviceRegion);
 
CGRect  cg1=CGRectMake(desx1, desy1, sw1, sh1);
UIImage *ui=[UIImage imageWithCGImage:ioffRegion];
 
CGContextDrawImage(当前context,cg1, ui.CGImage);
 
//清除缓冲
   CGColorSpaceRelease(iColorSpaceRegion);
   CGContextRelease(iDeviceRegion);
   CGImageRelease(ioffRegion);
   free(imageDataRegion);
//    iDeviceRegion=NULL;
//    imageDataRegion=0;
 
return ui;
}

UIImage 裁剪图片和等比列缩放图片的更多相关文章

  1. IOS 缩放图片常用方法

    /** * 指定Size压缩图片 (图片会压缩变形) * * @param image 原图 * @param size 压缩size * * @return 压缩后的图片 */ -(UIImage* ...

  2. TP5缩放图片加水印

    // 给图片增加水印文字 试验缩放图片,放大图片,加水印,加文字功能 public function doCreateImage1($data,$path) { $basePath = ROOT_PA ...

  3. Android代码中动态设置图片的大小(自动缩放),位置

    项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下: 1.布局文件 <RelativeLayout xmlns:android=&qu ...

  4. 在viewPager中双指缩放图片,双击缩放图片,单指拖拽图片

    我们就把这个问题叫做图片查看器吧,它的主要功能有: (项目地址:https://github.com/TZHANHONG/ImageViewer/releases/tag/1.0,里面的MyImage ...

  5. UIImage 图片处理:截图,缩放,设定大小,存储

    图片的处理大概就分 截图(capture), 缩放(scale),设定大小(resize), 存储(save)这几样比较好处理, 另外还有滤镜,擦试等, 以后再说在这个Demo code裡, 我写了几 ...

  6. 【转】java缩放图片、java裁剪图片代码工具类

    一首先看下效果 二工具类 三测试类 在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统 ...

  7. iOS裁剪,缩放图片白边问题解决办法

    几年没来了,感觉还是要写点啥,以后碰见问题 解决就写这吧,当是一个随时的笔记也好. iOS裁剪,缩放图片的代码网上也很多了,但是笔者出现了右边和下边出现白边的情况.出现白边的原因是给的size中的CG ...

  8. 使用PHP的GD2裁剪 + 缩放图片

    /** * 裁剪 + 缩放图片 * @param array $params 包含x,y,width,height,path * @return string */ public function t ...

  9. css如何实现图片响应式等比例缩放,裁剪

    <div class="bg_picWrapper"  :style="{backgroundImage:'url('+img+')'}">---- ...

随机推荐

  1. Eclipse+PyDev 安装和配置(转)

    Python开发有很多工具,其中Eclipse+Pydev 是最常见的一种.本文简单介绍Windows下Eclipse+PyDev 安装和配置. Eclipse 是一种基于 Java 的可扩展开源开发 ...

  2. Android设计模式系列(3)--SDK源码之单例模式

    单例模式,可以说是GOF的23种设计模式中最简单的一个.这个模式相对于其他几个模式比较独立,它只负责控制自己的实例化数量单一(而不是考虑为用户产生什么样的实例),很有意思,是一个感觉上很干净的模式,本 ...

  3. 引用、数组引用与指针引用、内联函数inline、四种类型转换运算符

    一.引用 (1).引用是给一个变量起别名 定义引用的一般格式:类型  &引用名 = 变量名: 例如:int a=1;  int  &b=a;// b是a的别名,因此a和b是同一个单元 ...

  4. 第二节 JVM优化应用以及知识总结

    在JVM中.假设98%的时间是用于GC且可用的HeapSize不足2%时将会抛出OOM异常:HeapSize最大不要超过可用物理内存的80%,一般-Xms –Xmx设置为同样,-Xmn设置为1/4的- ...

  5. 【转】搞清楚LzoCodec和LzopCodec

    使用LZO过程会发现它有两种压缩编码可以使用,即LzoCodec和LzopCodec,下面说说它们区别: LzoCodec比LzopCodec更快, LzopCodec为了兼容LZOP程序添加了如 b ...

  6. 中兴ZXV10 B860AV1.1 全TTL操作完美破解

    本文转自:http://www.znds.com/tv-496624-1-1.html 1)前期准备工作 1.1 拆开盒子,TTL接线,这个论坛里有好多其它帖子,就不再详细描述. 1.2 复制需要安装 ...

  7. 记一次线上MySQL数据库死锁问题

            最近线上项目报了一个MySQL死锁(DealLock)错误,虽说对业务上是没有什么影响的,由于自己对数据库锁这块了解不是很多,之前也没怎么的在线上碰到过.这次刚好遇到了,便在此记录一下 ...

  8. trk压力测试工具(测试tcp)

    wrk 是web站点压力测试工具 针对tcp协议的压力测试工具,没有找到合适的. 自己写一个,起名 trk.

  9. Linux系统编程--read/write

    . read/write read函数从打开的设备或文件中读取数据. #include <unistd.h> ssize_t read(int fd, void *buf, size_t ...

  10. mvc 返回list数据 页面 mode

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<实体命名空间& ...