本文转载至 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. 虚拟机运行Linux提示【此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态。】的问题

    虚拟机运行Linux提示[此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态.]的问题 换了台新笔记本,安装了虚拟机,导入以前的Linux系统镜像,出问题了. 提示以下错误信息: ...

  2. python pandas groupby

    转自 : https://blog.csdn.net/Leonis_v/article/details/51832916 pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对 ...

  3. vue 发送ajax请求

    一. 简介 1.vue本身不支持发送AJAX请求,需要使用vue-resource(vue1.0版本).axios(vue2.0版本)等插件实现 2.axios是一个基于Promise的HTTP请求客 ...

  4. Bitmap OutOfMemoryError

    在创建Bitmap的时候因为对象过多而没有即使回收,导致的内存不足: java.lang.OutOfMemoryError E/AndroidRuntime(21898):     at androi ...

  5. tornado部署

    1.为什么要运行多个tornado实例同步请求时,在应用处理过程中(如数据库查询,磁盘访问),服务器进程不能接受新请求.所以需要运行多个服务器进程实例.异步请求时,在应用处理时,服务器进程是非阻塞的, ...

  6. HDU 1863 畅通工程 克鲁斯卡尔算法

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. centos7下忘记mysql5.7密码

    才装完的mysql,转眼密码就忘记了,找了一圈的修改密码方法,做下记录! 编辑mysql配置文件. [root@localhost ~]# vi /etc/my.cnf 在[mysqld]配置节下新增 ...

  8. jquery插件-table转Json数据插件

    使 用开源插件Table-to-json: 官方地址:http://lightswitch05.github.io/table-to-json/ 功能说明:将js对象table转换成javascrip ...

  9. Handler实例

    Handler使用例1这个例子是最简单的介绍handler使用的,是将handler绑定到它所建立的线程中.本次实验完成的功能是:单击Start按钮,程序会开始启动线程,并且线程程序完成后延时1s会继 ...

  10. 在ubuntu下安装sourceinsight

    执行更新与安装 wine: # sudo apt-get update # sudo apt-get install wine 下载SourceInsight,用wine来安装: 执行:wine so ...