码迷,mamicode.com
首页 > 其他好文 > 详细

UIImage的几种初始化方法的对比

时间:2015-10-28 21:15:52      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

1、imageNamed:方法

imageNamed:是UIImage的一个类方法,它做的事情比我们看到的要稍微多一些。它的加载流程如下:

a. 系统回去检查系统缓存中是否存在该名字的图像,如果存在则直接返回。

b. 如果系统缓存中不存在该名字的图像,则会先加载到缓存中,在返回该对象。

观察上面的操作我们发现系统会缓存我们使用imageNamed:方法加载的图像时候,系统会自动帮我们缓存。这种机制适合于那种频繁用到界面贴图累的加载,但如果我们需要短时间内频繁的加载一些一次性的图像的话,最好不要使用这种方法。

2、imageWithContentsOfFile:和initWithContentsOfFile:方法

这两个方法跟前一个方法一样都是完成从文件加载图像的功能。但是不会经过系统缓存,直接从文件系统中加载并返回。

顺便提一下,当收到内存警告的时候,系统可能会将UIImage内部的存储图像的内存释放,下一次需要绘制的时候会重新去加载。

3、imageWithCGImage:scale:orientation:方法

该方面使用一个CGImageRef创建UIImage,在创建时还可以指定方法倍数以及旋转方向。当scale设置为1的时候,新创建的图像将和原图像尺寸一摸一样,而orientaion则可以指定新的图像的绘制方向。

三、UIImage的imageOrientation属性

UIImage有一个imageOrientation的属性,主要作用是控制image的绘制方向,共有以下8中方向:

  1. typedef NS_ENUM(NSInteger, UIImageOrientation) { 
  2.     UIImageOrientationUp,            // default orientation    
  3.     UIImageOrientationDown,          // 180 deg rotation        
  4.     UIImageOrientationLeft,          // 90 deg CCW      (编程发现官方文档中,left和right图像标反了,此处更正过来) 
  5.     UIImageOrientationRight,         // 90 deg CW    
  6.     UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip   
  7.     UIImageOrientationDownMirrored,  // horizontal flip 
  8.     UIImageOrientationLeftMirrored,  // vertical flip  
  9.     UIImageOrientationRightMirrored, // vertical flip  
  10. }; 

默认的方向是UIImageOrientationUp,这8种方向对应的绘制方如上面所示。我 们在日常使用中经常会碰到把iPhone相册中的照片导入到windows中,发现方向不对的问题就是与这个属性有关,因为导出照片的时候,写exif中 的方向信息时候没有考虑该方向的原因。既然这个属性可以控制image的绘制方向,那我们能不能通过改过这个属性来完成UIImage的旋转和翻转呢?带 着这个问题我们继续往下看。

四、UIImage的几个draw方法

UIImage的几个draw方法是用来绘制图像的利器,为什么这样说呢?因为它们在绘制图 像的时候会考虑当前图像的方向,即根据的imageOrientation绘制出不同的方向。由于图像是绘制在当前context中的,它同时还会考虑到 当前context的transform的变化。利于这两点我们就可以玩转图像的旋转和翻转了。

搜索了一些,目前网上大部分图像旋转都是通过创建CGBitmapContext,然后根据图像方向设置context的transform来实现的,这种方法要求对整个矩阵变化的过程都非常清楚,一个参数设置不多,出来的结果就会有问题。

下面我介绍一种实现起来简单方便的图像旋转方法,这种方法主要就是利用imageWithCGImage:scale:orientation:方法,指定不同的orientation来完成所需要的功能,先举个简单的例子:

假设一副图片显示为 技术分享,我们要向左旋转90°,那么转过之后应该就会显示为技术分享,即将原图从orientationUP转到orientationLeft即可。以此类推为不同的方向旋转,只需要注意看R的显示即可,这样整个旋转和翻转的实现过程中完全可以不用考虑Transform那些东西,是不是很简单。

下面是图像旋转和翻转的完整代码:

  1. // 
  2. //  UIImage+Rotate_Flip.h 
  3. //  SvImageEdit 
  4. // 
  5. //  Created by  maple on 5/14/13. 
  6. //  Copyright (c) 2013 smileEvday. All rights reserved. 
  7. // 
  8. // 
  9.   
  10. #import <UIKit/UIKit.h> 
  11.   
  12. @interface UIImage (Rotate_Flip) 
  13.   
  14. /* 
  15. * @brief rotate image 90 withClockWise 
  16. */ 
  17. - (UIImage*)rotate90Clockwise; 
  18.   
  19. /* 
  20. * @brief rotate image 90 counterClockwise 
  21. */ 
  22. - (UIImage*)rotate90CounterClockwise; 
  23.   
  24. /* 
  25. * @brief rotate image 180 degree 
  26. */ 
  27. - (UIImage*)rotate180; 
  28.   
  29. /* 
  30. * @brief rotate image to default orientation 
  31. */ 
  32. - (UIImage*)rotateImageToOrientationUp; 
  33.   
  34. /* 
  35. * @brief flip horizontal 
  36. */ 
  37. - (UIImage*)flipHorizontal; 
  38.   
  39. /* 
  40. * @brief flip vertical 
  41. */ 
  42. - (UIImage*)flipVertical; 
  43.   
  44. /* 
  45. * @brief flip horizontal and vertical 
  46. */ 
  47. - (UIImage*)flipAll; 
  48.   
  49.   
  50. @end 
  51.   
  52. UIImage+Rotate_Flip.h 
  1. // 
  2. //  UIImage+Rotate_Flip.m 
  3. //  SvImageEdit 
  4. // 
  5. //  Created by  maple on 5/14/13. 
  6. //  Copyright (c) 2013 smileEvday. All rights reserved. 
  7. // 
  8.   
  9. #import "UIImage+Rotate_Flip.h" 
  10.   
  11. @implementation UIImage (Rotate_Flip) 
  12.   
  13. /* 
  14. * @brief rotate image 90 with CounterClockWise 
  15. */ 
  16. - (UIImage*)rotate90CounterClockwise 
  17. { 
  18.     UIImage *image = nil; 
  19.     switch (self.imageOrientation) { 
  20.         case UIImageOrientationUp: 
  21.         { 
  22.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeft]; 
  23.             break; 
  24.         } 
  25.         case UIImageOrientationDown: 
  26.         { 
  27.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRight]; 
  28.             break; 
  29.         } 
  30.         case UIImageOrientationLeft: 
  31.         { 
  32.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDown]; 
  33.             break; 
  34.         } 
  35.         case UIImageOrientationRight: 
  36.         { 
  37.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUp]; 
  38.             break; 
  39.         } 
  40.         case UIImageOrientationUpMirrored: 
  41.         { 
  42.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRightMirrored]; 
  43.             break; 
  44.         } 
  45.         case UIImageOrientationDownMirrored: 
  46.         { 
  47.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeftMirrored]; 
  48.             break; 
  49.         } 
  50.         case UIImageOrientationLeftMirrored: 
  51.         { 
  52.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUpMirrored]; 
  53.             break; 
  54.         } 
  55.         case UIImageOrientationRightMirrored: 
  56.         { 
  57.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDownMirrored]; 
  58.             break; 
  59.         } 
  60.         default: 
  61.             break; 
  62.     } 
  63.      
  64.     return image; 
  65. } 
  66.   
  67. /* 
  68. * @brief rotate image 90 with Clockwise 
  69. */ 
  70. - (UIImage*)rotate90Clockwise 
  71. { 
  72.     UIImage *image = nil; 
  73.     switch (self.imageOrientation) { 
  74.         case UIImageOrientationUp: 
  75.         { 
  76.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRight]; 
  77.             break; 
  78.         } 
  79.         case UIImageOrientationDown: 
  80.         { 
  81.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeft]; 
  82.             break; 
  83.         } 
  84.         case UIImageOrientationLeft: 
  85.         { 
  86.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUp]; 
  87.             break; 
  88.         } 
  89.         case UIImageOrientationRight: 
  90.         { 
  91.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDown]; 
  92.             break; 
  93.         } 
  94.         case UIImageOrientationUpMirrored: 
  95.         { 
  96.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeftMirrored]; 
  97.             break; 
  98.         } 
  99.         case UIImageOrientationDownMirrored: 
  100.         { 
  101.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRightMirrored]; 
  102.             break; 
  103.         } 
  104.         case UIImageOrientationLeftMirrored: 
  105.         { 
  106.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDownMirrored]; 
  107.             break; 
  108.         } 
  109.         case UIImageOrientationRightMirrored: 
  110.         { 
  111.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUpMirrored]; 
  112.             break; 
  113.         } 
  114.         default: 
  115.             break; 
  116.     } 
  117.      
  118.     return image; 
  119. } 
  120.   
  121. /* 
  122. * @brief rotate image 180 degree 
  123. */ 
  124. - (UIImage*)rotate180 
  125. { 
  126.     UIImage *image = nil; 
  127.     switch (self.imageOrientation) { 
  128.         case UIImageOrientationUp: 
  129.         { 
  130.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDown]; 
  131.             break; 
  132.         } 
  133.         case UIImageOrientationDown: 
  134.         { 
  135.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUp]; 
  136.             break; 
  137.         } 
  138.         case UIImageOrientationLeft: 
  139.         { 
  140.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRight]; 
  141.             break; 
  142.         } 
  143.         case UIImageOrientationRight: 
  144.         { 
  145.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeft]; 
  146.             break; 
  147.         } 
  148.         case UIImageOrientationUpMirrored: 
  149.         { 
  150.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDownMirrored]; 
  151.             break; 
  152.         } 
  153.         case UIImageOrientationDownMirrored: 
  154.         { 
  155.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUpMirrored]; 
  156.             break; 
  157.         } 
  158.         case UIImageOrientationLeftMirrored: 
  159.         { 
  160.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRightMirrored]; 
  161.             break; 
  162.         } 
  163.         case UIImageOrientationRightMirrored: 
  164.         { 
  165.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeftMirrored]; 
  166.             break; 
  167.         } 
  168.         default: 
  169.             break; 
  170.     } 
  171.      
  172.     return image; 
  173. } 
  174.   
  175. /* 
  176. * @brief rotate image to default orientation 
  177. */ 
  178. - (UIImage*)rotateImageToOrientationUp 
  179. { 
  180.     CGSize size = CGSizeMake(self.size.width * self.scale, self.size.height * self.scale); 
  181.     UIGraphicsBeginImageContext(size); 
  182.     CGContextRef context = UIGraphicsGetCurrentContext(); 
  183.     CGContextClearRect(context, CGRectMake(0, 0, size.width, size.height)); 
  184.      
  185.     [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
  186.      
  187.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
  188.     UIGraphicsEndImageContext(); 
  189.      
  190.     return image; 
  191. } 
  192.   
  193. /* 
  194. * @brief flip horizontal 
  195. */ 
  196. - (UIImage*)flipHorizontal 
  197. { 
  198.     UIImage *image = nil; 
  199.     switch (self.imageOrientation) { 
  200.         case UIImageOrientationUp: 
  201.         { 
  202.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUpMirrored]; 
  203.             break; 
  204.         } 
  205.         case UIImageOrientationDown: 
  206.         { 
  207.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDownMirrored]; 
  208.             break; 
  209.         } 
  210.         case UIImageOrientationLeft: 
  211.         { 
  212.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRightMirrored]; 
  213.             break; 
  214.         } 
  215.         case UIImageOrientationRight: 
  216.         { 
  217.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeftMirrored]; 
  218.             break; 
  219.         } 
  220.         case UIImageOrientationUpMirrored: 
  221.         { 
  222.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUp]; 
  223.             break; 
  224.         } 
  225.         case UIImageOrientationDownMirrored: 
  226.         { 
  227.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDown]; 
  228.             break; 
  229.         } 
  230.         case UIImageOrientationLeftMirrored: 
  231.         { 
  232.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRight]; 
  233.             break; 
  234.         } 
  235.         case UIImageOrientationRightMirrored: 
  236.         { 
  237.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeft]; 
  238.             break; 
  239.         } 
  240.         default: 
  241.             break; 
  242.     } 
  243.      
  244.     return image; 
  245. } 
  246.   
  247. /* 
  248. * @brief flip vertical 
  249. */ 
  250. - (UIImage*)flipVertical 
  251. { 
  252.     UIImage *image = nil; 
  253.     switch (self.imageOrientation) { 
  254.         case UIImageOrientationUp: 
  255.         { 
  256.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDownMirrored]; 
  257.             break; 
  258.         } 
  259.         case UIImageOrientationDown: 
  260.         { 
  261.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUpMirrored]; 
  262.             break; 
  263.         } 
  264.         case UIImageOrientationLeft: 
  265.         { 
  266.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeftMirrored]; 
  267.             break; 
  268.         } 
  269.         case UIImageOrientationRight: 
  270.         { 
  271.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRightMirrored]; 
  272.             break; 
  273.         } 
  274.         case UIImageOrientationUpMirrored: 
  275.         { 
  276.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationDown]; 
  277.             break; 
  278.         } 
  279.         case UIImageOrientationDownMirrored: 
  280.         { 
  281.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationUp]; 
  282.             break; 
  283.         } 
  284.         case UIImageOrientationLeftMirrored: 
  285.         { 
  286.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationLeft]; 
  287.             break; 
  288.         } 
  289.         case UIImageOrientationRightMirrored: 
  290.         { 
  291.             image = [UIImage imageWithCGImage:self.CGImage scale:1 orientation:UIImageOrientationRight]; 
  292.             break; 
  293.         } 
  294.         default: 
  295.             break; 
  296.     } 
  297.      
  298.     return image; 
  299. } 
  300.   
  301. /* 
  302. * @brief flip horizontal and vertical 
  303. */ 
  304. - (UIImage*)flipAll 
  305. { 
  306.     return [self rotate180]; 
  307. } 
  308.   
  309. @end 
  310.   
  311. UIImage+Rotate_Flip.m 

以上只是实现了图像的顺时针90°,逆时针90°,180°旋转,以及水平翻转,数值翻转等。至 于任意角度旋转怎么实现?其实也很简单,留着给大家思考吧。虽然我们可以通过orientation这种方法简单的完成图像旋转,但是如果有时间的话还是 建议大家尽量的看一下那种通过transform来完成旋转的代码,你会彻底搞清楚旋转矩阵是怎么回事儿。当然程序中使用的时候推荐使用我上面提供的这种 方法,因为不涉及真实的旋转操作,速度会快很多。

通过上面的小例子,我们可以看出越高级别的API帮助我们做的事情就越多,越底层的API提 供了更多的灵活性,但同时也带来了很多需要我们处理的东西。再编程的过程中尽量的使用高级别的API,同时最好能搞懂底层的实现机制。这样我们的程序才会 更高效,出了问题才知道去哪里查找。

UIImage的几种初始化方法的对比

标签:

原文地址:http://www.cnblogs.com/iOStingting/p/4918559.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!