您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UIImage 類別方法總整理:建立、繪製、動畫與可拉伸圖片

編輯:iOS 開發綜合

做專案幾乎一定會用到 UIImage。這個類別繼承自 NSObject,方法數量不少,以下依照「建立、初始化、繪製、屬性」四個面向整理,方便日後查閱。

最常用:用檔名取圖

+ (UIImage *)imageNamed:(NSString *)name;

適用於已加入專案 bundle 的圖片。系統會快取解碼後的結果,因此重複取用同名圖片時效能較好——但也正因為有快取,它會持續佔用記憶體,不適合載入大量暫時性的大圖。

建立新圖片

+ (UIImage *)imageWithContentsOfFile:(NSString *)path;      // 從指定路徑載入
+ (UIImage *)imageWithData:(NSData *)data;                  // 從 NSData 建立
+ (UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale;  // 多一個縮放比例

+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage;          // 由 Quartz 2D 物件建立
+ (UIImage *)imageWithCGImage:(CGImageRef)imageRef
                        scale:(CGFloat)scale
                  orientation:(UIImageOrientation)orientation;   // 指定比例與方向

+ (UIImage *)imageWithCIImage:(CIImage *)ciImage;           // 由 Core Image 物件建立
+ (UIImage *)imageWithCIImage:(CIImage *)ciImage
                        scale:(CGFloat)scale
                  orientation:(UIImageOrientation)orientation;

- (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets;  // 指定對齊內縮

其中 imageWithData:scale: 的 scale 參數特別有用:從網路下載的圖片若帶有 @2x、@3x 標記,可以據此指定正確比例,避免在高解析度螢幕上被錯誤放大而模糊。

動態圖片

+ (UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)duration;
+ (UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration;

+ (UIImage *)animatedResizableImageNamed:(NSString *)name
                               capInsets:(UIEdgeInsets)capInsets
                                duration:(NSTimeInterval)duration;

+ (UIImage *)animatedResizableImageNamed:(NSString *)name
                               capInsets:(UIEdgeInsets)capInsets
                           resizingMode:(UIImageResizingMode)resizingMode
                                duration:(NSTimeInterval)duration;

animatedImageNamed: 會自動依序載入 name0.png、name1.png、name2.png… 這樣的序列檔。後兩個方法則搭配可拉伸圖片,讓動畫的邊框保持不變、只有中間區塊伸展,很適合做按鈕的按下動畫。

可拉伸圖片

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
                            resizingMode:(UIImageResizingMode)resizingMode;

resizingMode 可選平鋪(tile)或拉伸(stretch)。自 iOS 7 起,也可以用 resizableImageWithCapInsets:resizingMode: 配合 UIEdgeInsetsMake 做出「九宮格」效果,讓圓角按鈕背景能自由變形而不失真。

初始化方法

以下初始化器的作用與上面對應的類別方法相同,只是以實例形式呼叫:

- initWithContentsOfFile:
- initWithData:
- initWithData:scale:
- initWithCGImage:
- initWithCGImage:scale:orientation:
- initWithCIImage:
- initWithCIImage:scale:orientation:

繪製相關

- (void)drawAtPoint:(CGPoint)point;
// 以 point 為左上角原點繪製整張圖

- (void)drawAtPoint:(CGPoint)point
          blendMode:(CGBlendMode)blendMode
              alpha:(CGFloat)alpha;
// 加上自訂混合模式與透明度

- (void)drawInRect:(CGRect)rect;
// 在指定矩形內繪製,可自動縮放

- (void)drawInRect:(CGRect)rect
         blendMode:(CGBlendMode)blendMode
             alpha:(CGFloat)alpha;

- (void)drawAsPatternInRect:(CGRect)rect;
// 在矩形內以平鋪方式填滿

這幾個繪製方法必須在 UIGraphicsGetCurrentContext() 有效的情況下呼叫,也就是說通常得放在 drawRect: 裡,或自行包在 UIGraphicsBeginImageContext 與 UIGraphicsEndImageContext 之間。

常用屬性

  • imageOrientation:圖片方向。
  • size:以點為單位的尺寸(非像素)。
  • scale:縮放比例。
  • resizingMode:可拉伸圖片的變形方式。
  • CGImage:底層的 Quartz image 物件。
  • CIImage:底層的 Core Image 物件。
  • images:動態圖片的組成影格陣列。
  • duration:動態圖片播放一輪的時間。
  • capInsets:圖片上被保留不變形的邊緣區域。
  • alignmentRectInsets:對齊時的內縮區域。

讀一遍文件的意義,往往不在於當下就用得上,而是知道「這個類別裡有什麼、需要時該去哪裡找」。等到真正遇到需求,翻起來的速度會快很多。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved