自訂 UIImageView 控件
一、實作思路
Quartz2D 最大的用武之地,莫過於自訂 View(自訂 UI 控件)。當系統內建的 View 無法滿足我們的需求時,就得靠自訂 View 來救場。而用 Quartz2D 自訂 View,最合適的切入點就是模仿系統 UIImageView 的使用方式。需求驅動開發——我們就照著系統 imageview 的使用流程走一遍:1. 創建;2. 設置圖片;3. 設置 frame;4. 把這個自訂 view 加到界面上(在自訂 View 中,需要一個 image 屬性來接收 image 圖片參數 → 5);5. 添加一個 image 屬性(拿到 image 之後,自然得把它渲染出來。怎麼渲染?自訂 view 如何把圖片顯示出來?→ 把圖片畫出來,因此需要重寫自訂 View 的 drawRect: 方法);6. 重寫自訂 View 的 drawRect: 方法,在該方法內部把圖形畫出來。二、程式碼實作
先看系統自帶 UIImageView 的用法: // // YYViewController.m // // // Created by apple on 14-6-22. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" @interface YYViewController () @end @implementation YYViewController - (void)viewDidLoad { [super viewDidLoad]; //系統的UIImageview的使用 // 1.創建一個UIImageView UIImageView *iv=[[UIImageView alloc]init]; // 2.設置圖片 iv.image=[UIImage imageNamed:@"me"]; // 3.設置frame iv.frame=CGRectMake(100, 100, 100, 100); // 4.把創建的自定義的view添加到界面上 [self.view addSubview:iv]; } @end
實現效果:

接著用 Quartz2D 自訂 View,程式碼如下。先新建一個自訂類別,讓它繼承自 UIView。YYimageView.h 的程式碼如下:
1 // 2 // YYimageView.h 3 // 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import10 11 @interface YYimageView : UIView 12 @property(nonatomic,strong)UIImage *image; 13 @end
在自訂類別的實作中,重寫 drawRect: 方法來繪製並渲染圖形。YYimageView.m 的程式碼如下:
1 // 2 // YYimageView.m 3 // 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYimageView.h" 10 11 @implementation YYimageView 12 13 //重寫drawRect:方法 14 - (void)drawRect:(CGRect)rect 15 { 16 [self.image drawInRect:rect]; 17 } 18 19 @end
在主控制器中,模仿系統自帶 UIImageView 的使用流程,實現同樣的效果。
1 // 2 // YYViewController.m 3 // 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import "YYimageView.h" 11 12 @interface YYViewController () 13 14 @end 15 16 @implementation YYViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 // //系統的UIImageview的使用 23 //// 1.創建一個UIImageView 24 // UIImageView *iv=[[UIImageView alloc]init]; 25 //// 2.設置圖片 26 // iv.image=[UIImage imageNamed:@"me"]; 27 //// 3.設置frame 28 // iv.frame=CGRectMake(100, 100, 100, 100); 29 //// 4.把創建的自定義的view添加到界面上 30 // [self.view addSubview:iv]; 31 32 33 //自定義UIImageView 34 //1.創建 35 //2.設置圖片 36 //3.設置frame 37 //4.把創建的自定義的view添加到界面上 38 YYimageView *yyiv=[[YYimageView alloc]init]; 39 yyiv.image=[UIImage imageNamed:@"me"]; 40 yyiv.frame=CGRectMake(100, 100, 100, 100); 41 [self.view addSubview:yyiv]; 42 43 } 44 @end
三、完善
存在的問題是什麼?
我們在界面上添加一個按鈕,要求點擊後能實現圖片的切換。
1 // 2 // YYViewController.m 3 // 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import "YYimageView.h" 11 12 @interface YYViewController () 13 @property(nonatomic,strong)UIImageView *imageView; 14 @end 15 16 @implementation YYViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 //系統的UIImageview的使用 23 // 1.創建一個UIImageView 24 UIImageView *iv=[[UIImageView alloc]init]; 25 // 2.設置圖片 26 iv.image=[UIImage imageNamed:@"me"]; 27 // 3.設置frame 28 iv.frame=CGRectMake(100, 100, 100, 100); 29 // 4.把創建的自定義的view添加到界面上 30 [self.view addSubview:iv]; 31 self.imageView=iv; 32 33 34 //自定義UIImageView 35 //1.創建 36 //2.設置圖片 37 //3.設置frame 38 //4.把創建的自定義的view添加到界面上 39 // YYimageView *yyiv=[[YYimageView alloc]init]; 40 // yyiv.image=[UIImage imageNamed:@"me"]; 41 // yyiv.frame=CGRectMake(100, 100, 100, 100); 42 // [self.view addSubview:yyiv]; 43 44 //添加一個button按鈕,當點擊button按鈕的時候,切換圖片 45 UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)]; 46 [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 47 [btn setTitle:@"點擊切換" forState:UIControlStateNormal]; 48 [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; 49 [self.view addSubview:btn]; 50 51 } 52 53 -(void)btnClick 54 { 55 UIImage *image=[UIImage imageNamed:@"psb.jpeg"]; 56 self.imageView.image=image; 57 } 58 @end
點擊按鈕後,圖片便完成切換。

完善後的程式碼如下。
主控制器中,YYViewController.m 的程式碼:
1 // 2 // YYViewController.m 3 // 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import "YYimageView.h" 11 12 @interface YYViewController () 13 @property(nonatomic,strong)UIImageView *imageView; 14 @property(nonatomic,strong)YYimageView *yyimageView; 15 @end 16 17 @implementation YYViewController 18 19 - (void)viewDidLoad 20 { 21 [super viewDidLoad]; 22 23 // //系統的UIImageview的使用 24 //// 1.創建一個UIImageView 25 // UIImageView *iv=[[UIImageView alloc]init]; 26 //// 2.設置圖片 27 // iv.image=[UIImage imageNamed:@"me"]; 28 //// 3.設置frame 29 // iv.frame=CGRectMake(100, 100, 100, 100); 30 //// 4.把創建的自定義的view添加到界面上 31 // [self.view addSubview:iv]; 32 // self.imageView=iv; 33 34 35 //自定義UIImageView 36 //1.創建 37 //2.設置圖片 38 //3.設置frame 39 //4.把創建的自定義的view添加到界面上 40 YYimageView *yyiv=[[YYimageView alloc]init]; 41 yyiv.image=[UIImage imageNamed:@"me"]; 42 yyiv.frame=CGRectMake(100, 100, 100, 100); 43 [self.view addSubview:yyiv]; 44 self.yyimageView=yyiv; 45 46 //添加一個button按鈕,當點擊button按鈕的時候,切換圖片 47 UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)]; 48 [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 49 [btn setTitle:@"點擊切換" forState:UIControlStateNormal]; 50 [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; 51 [self.view addSubview:btn]; 52 53 } 54 55 -(void)btnClick 56 { 57 NSLog(@"按鈕被點擊了"); 58 UIImage *image=[UIImage imageNamed:@"psb.jpeg"]; 59 // self.imageView.image=image; 60 self.yyimageView.image=image; 61 } 62 @end
YYimageView.m 的程式碼:
1 // 2 // YYimageView.m 3 // 4 // 5 // Created by apple on 14-6-22. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYimageView.h" 10 11 @implementation YYimageView 12 13 //重寫drawRect:方法 14 - (void)drawRect:(CGRect)rect 15 { 16 [self.image drawInRect:rect]; 17 } 18 19 //重寫set方法 20 -(void)setImage:(UIImage *)image 21 { 22 _image=image; 23 [self setNeedsDisplay]; 24 } 25 @end
繪製基本圖形
一、簡單說明
圖形上下文(Graphics Context):本質上是一個 CGContextRef 類型的資料。圖形上下文的作用:保存繪圖資訊與繪圖狀態,並決定繪製的輸出目標(也就是畫到哪裡去?輸出品可以是 PDF 檔、Bitmap,或顯示器的視窗)。

同一套繪圖序列,只要指定不同的 Graphics Context,就能把相同的圖像繪製到不同的目標上。

Quartz2D 提供了以下幾種類型的 Graphics Context:
Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Graphics Context
Printer Graphics Context
只要上下文不同,繪製的位置也就不同。
本文說明如何把圖片繪製到 Bitmap 上,也就是要生成一張圖片,讓上面保存著繪圖資訊。Bitmap 就是圖片,相當於系統的 UIImage——一個 UIImage 本質上就是一個 Bitmap。
二、怎麼把圖片繪製到 Bitmap 上?
注意:不能在 drawRect: 方法中直接取得 Bitmap 的上下文,必須由我們自己來創建。
程式碼示例:
8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property (weak, nonatomic) IBOutlet UIImageView *iv; 13 @end 14 15 @implementation YYViewController 16 17 - (void)viewDidLoad 18 { 19 [super viewDidLoad]; 20 21 //加載圖片 22 //0.創建一個Bitmap上下文 23 //c語言的方法 24 // CGBitmapContextCreate(<#void *data#>, <#size_t width#>, <#size_t height#>, <#size_t bitsPerComponent#>, <#size_t bytesPerRow#>, <#CGColorSpaceRef space#>, <#CGBitmapInfo bitmapInfo#>) 25 //oc中封裝的方法 26 //方法1 27 // UIGraphicsBeginImageContext(<#CGSize size#>); 28 //方法2 29 UIGraphicsBeginImageContextWithOptions( CGSizeMake(200, 200), NO, 0); 30 //1.獲取bitmap上下文 31 CGContextRef ctx = UIGraphicsGetCurrentContext(); 32 //2.繪圖(畫一個圓) 33 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 100, 100)); 34 //3.渲染 35 CGContextStrokePath(ctx); 36 //4.獲取生成的圖片 37 UIImage *image=UIGraphicsGetImageFromCurrentImageContext(); 38 //5.顯示生成的圖片到imageview 39 self.iv.image=image; 40 //6.保存繪制好的圖片到文件中 41 //先將圖片轉換為二進制數據,然後再將圖片寫到文件中 42 // UIImageJPEGRepresentation(image, 1); //第二個參數為保存的圖片的效果 43 NSData *data=UIImagePNGRepresentation(image); 44 [data writeToFile:@"/Users/apple/Desktop/abc.png" atomically:YES]; 45 } 46 47 - (void)didReceiveMemoryWarning 48 { 49 [super didReceiveMemoryWarning]; 50 // Dispose of any resources that can be recreated. 51 } 52 53 @end
程式執行效果:

程式執行完畢後,會在指定位置生成一個 abc.png 圖片。

補充說明:
1. 創建 Bitmap 圖形上下文的方法//方法1 UIGraphicsBeginImageContext(<#CGSize size#>);
//方法2 UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
這兩個方法都能用來創建上下文,但用第一個方法生成的圖片,其清晰度與質量不如第二種方法。方法 2 接收三個參數:
CGSize size:指定將來創建出來的 bitmap 的大小。
BOOL opaque:預設為 YES,代表不透明,位圖中沒有繪製的區域會以黑色顯示;設為 NO 則代表透明,未繪製的區域會以透明顯示;它主要用作繪圖時進行效能優化的開關。
創建出來的 bitmap 就對應一個 UIImage 對象。