服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS应用开发中对UIImage进行截取和缩放的方法详解

iOS应用开发中对UIImage进行截取和缩放的方法详解

2021-01-17 20:33李刚 IOS

这篇文章主要介绍了iOS应用开发中对UIImage进行截取和缩放的方法,分别讲解了如何截取指定区域大小的UIImage以及缩放到指定大小和等比缩放的具体操作过程,需要的朋友可以参考下

截取uiimage指定大小区域
最近遇到这样的需求:从服务器获取到一张照片,只需要显示他的左半部分,或者中间部分等等。也就是截取uiimage指定大小区域。

uiimage扩展:

我的解决方案是对uiimage进行扩展。通过cgimageref和cgimage完成截取,调用的方法是:cgimagecreatewithimageinrect。扩展类叫uiimage+crop,具体代码如下:

uiimage+crop.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#import <uikit/uikit.h>
 
typedef ns_enum(nsinteger, xycropimagestyle){
  xycropimagestyleright        =0,   // 右半部分
  xycropimagestylecenter       =1,   // 中间部分
  xycropimagestyleleft        =2,   // 左半部分
  xycropimagestylerightoneofthird   =3,   // 右侧三分之一部分
  xycropimagestylecenteroneofthird  =4,   // 中间三分之一部分
  xycropimagestyleleftoneofthird   =5,   // 左侧三分之一部分
  xycropimagestylerightquarter    =6,   // 右侧四分之一部分
  xycropimagestylecenterrightquarter =7,   // 中间右侧四分之一部分
  xycropimagestylecenterleftquarter  =8,   // 中间左侧四分之一部分
  xycropimagestyleleftquarter     =9,   // 左侧四分之一部分
};
 
@interface uiimage (crop)
- (uiimage *)imagebycroppingwithstyle:(xycropimagestyle)style;
 
@end
uiimage+crop.m
 
#import "uiimage+crop.h"
 
@implementation uiimage (crop)
 
- (uiimage *)imagebycroppingwithstyle:(xycropimagestyle)style
{
  cgrect rect;
  switch (style) {
    case xycropimagestyleleft:
      rect = cgrectmake(0, 0, self.size.width/2, self.size.height);
      break;
    case xycropimagestylecenter:
      rect = cgrectmake(self.size.width/4, 0, self.size.width/2, self.size.height);
      break;
    case xycropimagestyleright:
      rect = cgrectmake(self.size.width/2, 0, self.size.width/2, self.size.height);
      break;
    case xycropimagestyleleftoneofthird:
      rect = cgrectmake(0, 0, self.size.width/3, self.size.height);
      break;
    case xycropimagestylecenteroneofthird:
      rect = cgrectmake(self.size.width/3, 0, self.size.width/3, self.size.height);
      break;
    case xycropimagestylerightoneofthird:
      rect = cgrectmake(self.size.width/3*2, 0, self.size.width/3, self.size.height);
      break;
    case xycropimagestyleleftquarter:
      rect = cgrectmake(0, 0, self.size.width/4, self.size.height);
      break;
    case xycropimagestylecenterleftquarter:
      rect = cgrectmake(self.size.width/4, 0, self.size.width/4, self.size.height);
      break;
    case xycropimagestylecenterrightquarter:
      rect = cgrectmake(self.size.width/4*2, 0, self.size.width/4, self.size.height);
      break;
    case xycropimagestylerightquarter:
      rect = cgrectmake(self.size.width/4*3, 0, self.size.width/4, self.size.height);
      break;
    default:
      break;
  }
  cgimageref imageref = self.cgimage;
  cgimageref imagepartref = cgimagecreatewithimageinrect(imageref, rect);
  uiimage *cropimage = [uiimage imagewithcgimage:imagepartref];
  cgimagerelease(imagepartref);
  return cropimage;
}

实际运用:

简单测试一下,看看有没有实现我们想要的效果。首先,先加载一个完整的uiimageview。这个应该不难。代码如下:

?
1
2
3
4
5
uiimageview *imgview = [[uiimageview alloc] init];
imgview.frame = cgrectmake((screen.width - 226) / 2, 100, 226, 106);
uiimage *image = [uiimage imagenamed:@"ganggang"];
imgview.image = image;
[self.view addsubview:imgview];

运行一下:

iOS应用开发中对UIImage进行截取和缩放的方法详解

要对uiimage进行裁剪,首先导入头文件:

?
1
#import "uiimage+crop.h"

在上面uiimage *image = [uiimage imagenamed:@"ganggang"];这段代码之后加上下面这句:

?
1
image = [image imagebycroppingwithstyle:xycropimagestyleleft];

xycropimagestyleleft是截取照片的左半部分。效果如下:

iOS应用开发中对UIImage进行截取和缩放的方法详解

截取成功,还可以截取其他区域的,只需要传入不同的xycropimagestyle即可实现。

uiimage等比缩放
前面讲了截取uiimage指定大小区域,很方便的截取uiimage。今天要和大家分享的是uiimage的缩放。

两种缩放:

  • 缩放到指定大小,也就是指定的size.
  • 等比缩放。

1.缩放到指定大小

?
1
2
3
4
5
6
7
8
- (uiimage*)imagecompresswithsimple:(uiimage*)image scaledtosize:(cgsize)size
{
  uigraphicsbeginimagecontext(size);
  [image drawinrect:cgrectmake(0,0,size.width,size.height)];
  uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext();
  uigraphicsendimagecontext();
  return newimage;
}

2.等比缩放

(1)通过缩放系数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
- (uiimage*)imagecompresswithsimple:(uiimage*)image scale:(float)scale
{
  cgsize size = image.size;
  cgfloat width = size.width;
  cgfloat height = size.height;
  cgfloat scaledwidth = width * scale;
  cgfloat scaledheight = height * scale;
  uigraphicsbeginimagecontext(size); // this will crop
  [image drawinrect:cgrectmake(0,0,scaledwidth,scaledheight)];
  uiimage* newimage= uigraphicsgetimagefromcurrentimagecontext();
  uigraphicsendimagecontext();
  return newimage;
}

scale是缩放系数 。

(2)通过计算得到缩放系数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
- (uiimage*)imagebyscalingandcroppingforsize:(cgsize)targetsize
{
 
  uiimage *sourceimage = [uiimage imagenamed:@"test.jpg"];
  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; // scale to fit height
    else
      scalefactor = heightfactor; // scale to fit width
 
    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;
    }
  }
 
  uigraphicsbeginimagecontext(targetsize); // this will crop
  cgrect thumbnailrect = cgrectzero;
  thumbnailrect.origin = thumbnailpoint;
  thumbnailrect.size.width= scaledwidth;
  thumbnailrect.size.height = scaledheight;
  [sourceimage drawinrect:thumbnailrect];
  newimage = uigraphicsgetimagefromcurrentimagecontext();
 
  if(newimage == nil)
    nslog(@"could not scale image");
  //pop the context to get back to the default
  uigraphicsendimagecontext();
 
  return newimage;
 
}

 

延伸 · 阅读

精彩推荐
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25