本文转载至 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. Python的ipython的安装

    IPython是Python 的原生交互式 shell 的增强版,可以完成许多不同寻常的任务,比如帮助实现并行化计算:主要使用它提供的交互性帮助,比如代码着色.改进了的命令行回调.制表符完成.宏功能以 ...

  2. Oracle开发者守则

    下面为Oracle大师级语录: Oracle Database developers should follow is to do everything they can in SQL. What t ...

  3. AutoFac文档7(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 适配器 和 装饰器 A ...

  4. systemctl 配置mysql 开机启动

    在centos 7 环境下对服务的管理已经不再用service 命令了,而是改为systemctl 命令来管理服务. 一.创建systemctl 的对mysql服务的配置文件: touch /usr/ ...

  5. Atitit.软件开发概念说明--io系统区--特殊文件名称保存最佳实践文件名称编码...filenameEncode

    Atitit.软件开发概念说明--io系统区--特殊文件名称保存最佳实践文件名称编码...filenameEncode 不个网页title保存成个个文件的时候儿有无效字符的问题... 通常两个处理方式 ...

  6. atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html js --attilax总结

    atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html  js --attilax总结 4. 鼠标越过动态图片切换实现 1 4.1. 优先模式::css模式... 1 4.2. 其次 ...

  7. 18. Subsets II【medium】

    Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each elem ...

  8. linux下挂载win7的共享文件夹

    由于跨平台开发的需要,需要在Linux和windows之间共享文件夹,所以找了一下方法,我试验了两种都可以使用. 首先声明一下我使用的是VMware10.CentOS6.2 一.手动操作 1.按照下图 ...

  9. ubuntu 16.04LTS

    安装出现"server64 busybox-initramfs安装失败" 这个是BUG,解决方法一:初次安装选择语言时,使用English,在后面还会有一个选择语言的界面,这时候再 ...

  10. 非常多学ThinkPHP的新手会遇到的问题

    在模板传递变量的时候,非常多视频教程都使用$v.channel的方式.例如以下: <a href="{:U('Chat/set',array('id'=>$v.channel)) ...