本文转载至 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. Android开发优化之——从代码角度进行优化

    通常我们写程序,都是在项目计划的压力下完成的,此时完成的代码可以完成具体业务逻 辑,但是性能不一定是最优化的.一般来说,优秀的程序员在写完代码之后都会不断的对代码进行重构.重构的好处有很多,其中一点, ...

  2. 安卓入门 使用android创建一个项目 从启动activity中响应按钮事件 启动另一个activity 并传递参数

    启动android studio创建一个新项目 public void sendMessage(View view){ Intent intent=new Intent(this,DispalyMes ...

  3. 峰值因子,峰均比,Reference Level

    峰值因子(CREST Factor,CF)与 峰均比( Peak-to-Average Ratio,PAR) 对于一个波形信号,在一段时间内信号幅度峰值比上信号幅度的有效值即为信号的峰值因子,它表征了 ...

  4. numpy 文件存取 npy、npz

    转处:http://blog.csdn.net/pipisorry/article/details/39088003 NumPy提供了多种文件操作函数方便我们存取数组内容. 文件存取的格式:二进制和文 ...

  5. mongodb查询实练

    1.mongodb中如何查询 (a=1 or b=2) and (c=3 or d=4)//格式:db.collection.find({"$and":[{第一个条件},{第二个条 ...

  6. wechat talk

    https://github.com/13770344697/nanjingzhongan https://github.com/fayuanliu/wxRobot https://github.co ...

  7. 李洪强iOS经典面试题36-简单介绍 ARC 以及 ARC 实现的原理

    李洪强iOS经典面试题36-简单介绍 ARC 以及 ARC 实现的原理   问题 简单介绍 ARC 以及 ARC 实现的原理. 考查点 ARC 是苹果在 WWDC 2011 提出来的技术,因此很多新入 ...

  8. js 内置对象常用方法

    1 内容概述 js包含一些内置对象,如Array,Function,String等,这些是基本的,常用的js类,所以了解它们十分重要:把他们的方法,用例子和文字简要的记录下来,方便今后参看. 2 Ar ...

  9. getAttribute()方法

    http://www.imooc.com/code/1587 getAttribute()方法 通过元素节点的属性名称获取属性的值. 语法: elementNode.getAttribute(name ...

  10. hdu3948(后缀数组)

    题意:给一串字符,需要你求不相同的回文子串个数....... 同ural1297,链接:http://www.cnblogs.com/ziyi--caolu/archive/2013/06/09/31 ...