成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

iOS開發中關于UIImage的知識點總結

移動開發 iOS
UIImage是iOS中層級比較高的一個用來加載和繪制圖像的一個類,更底層的類還有 CGImage,以及iOS5.0以后新增加的CIImage。今天我們主要聊一聊UIImage的三個屬性: imageOrientation, size, scale,幾個初始化的方法: imageNamed,imageWithContentsOfFile,以及繪制Image的幾個draw開頭的方法。

UIImage是iOS中層級比較高的一個用來加載和繪制圖像的一個類,更底層的類還有 CGImage,以及iOS5.0以后新增加的CIImage。今天我們主要聊一聊UIImage的三個屬性: imageOrientation, size, scale,幾個初始化的方法: imageNamed,imageWithContentsOfFile,以及繪制Image的幾個draw開頭的方法。

一、UIImage的size,scale屬性

先想一個問題“一個圖像的尺寸到底是多大呢?”

***反應可能就是image.size,恭喜你答錯了,正確的答案是圖像的實際的尺寸(像 素)等于image.size乘以image.scale。如果做過界面貼圖的話你可能經常會需要準備至少兩套圖,一套1倍圖,一套圖已@2x命名的二倍 圖。這樣當我們的程序運行在retina屏幕的時候系統就會自動的去加載@2x的圖片,它的size將和一倍圖加載進來的size相等,但是scale卻 置為2,這點大家可以做個簡單的小測試驗證一下。然我們再深入一點兒為什么不直接加載到成二倍的尺寸呢,原因很簡單因為我們在界面布局中邏輯坐標系中的 (單位是point),而實際的繪制都是在設備坐標系(單位是pixel)進行的,系統會自動幫我們完成從point到pixel之間的轉化。其實這個比 例也就剛好和UIScreen中的scale對應,這樣整條scale的線就可以串通了。

二、UIImage的幾種初始化方法的對比

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來完成所需要的功能,先舉個簡單的例子:

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

以上只是實現了圖像的順時針90°,逆時針90°,180°旋轉,以及水平翻轉,數值翻轉等。至 于任意角度旋轉怎么實現?其實也很簡單,留著給大家思考吧。雖然我們可以通過orientation這種方法簡單的完成圖像旋轉,但是如果有時間的話還是 建議大家盡量的看一下那種通過transform來完成旋轉的代碼,你會徹底搞清楚旋轉矩陣是怎么回事兒。當然程序中使用的時候推薦使用我上面提供的這種 方法,因為不涉及真實的旋轉操作,速度會快很多。

通過上面的小例子,我們可以看出越高級別的API幫助我們做的事情就越多,越底層的API提 供了更多的靈活性,但同時也帶來了很多需要我們處理的東西。再編程的過程中盡量的使用高級別的API,同時***能搞懂底層的實現機制。這樣我們的程序才會 更高效,出了問題才知道去哪里查找。

來自www.cnblogs.com/smileEvday/archive/2013/05/14/UIImage.html

責任編輯:閆佳明 來源: itjhwd
相關推薦

2009-08-06 17:42:32

C#知識點

2016-12-21 09:55:55

面試JavaScrip總結

2013-01-06 09:52:43

SQLite

2013-04-09 16:04:06

iOS開發SQLite知識總結

2010-06-17 16:42:04

UML

2010-08-18 10:52:46

Linux筆試

2022-07-20 00:15:48

SQL數據庫編程語言

2020-12-28 08:13:01

前端開發技術熱點

2020-12-28 08:16:30

前端開發技術熱點

2020-07-01 17:25:28

Redis數據庫內存

2015-04-21 09:28:29

2020-12-24 13:32:31

大數據數據分析SQL

2017-12-08 14:26:19

Android面試知識點總結

2014-03-12 10:13:00

iOSSEL對象

2021-04-13 08:25:12

測試開發Java注解Spring

2012-04-23 15:49:04

2022-08-16 15:17:37

機器學習算法模型

2020-06-19 16:25:19

MySQL日志文件數據庫

2021-06-29 15:56:39

MYSQL開發數據庫

2020-02-07 09:59:29

Python異常處理語言
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 9久9久9久女女女九九九一九 | 久久久99国产精品免费 | 亚洲高清视频一区二区 | 一区二区三区成人 | zzzwww在线看片免费 | 国产精品乱码一区二区三区 | 色婷婷在线视频 | 亚洲永久| 成人国内精品久久久久一区 | 欧美一区二区三区在线看 | 精品欧美一区二区三区久久久 | 男女免费观看在线爽爽爽视频 | 中文字幕成人在线 | 人人九九| 一区二区三 | 亚洲精品久久久蜜桃 | 亚洲第一在线视频 | 国产精品久久亚洲 | 欧洲一区二区在线 | 亚洲一区二区在线播放 | 久久精品网 | 欧美一区二区在线观看 | 日韩美女在线看免费观看 | 亚洲久草视频 | 国产精品久久久久久久久久久久午夜片 | 国产精品综合色区在线观看 | 黄色片免费看视频 | www.日本精品| 99在线国产 | 亚洲激情视频在线 | 九九热久久免费视频 | 久久久久国产精品一区三寸 | 色伊人| 国产一区二区三区在线免费 | 日一区二区 | 国产精品爱久久久久久久 | 国产成人精品网站 | av片在线免费看 | 在线精品亚洲欧美日韩国产 | 看a网站| 国产成人a亚洲精品 |