標籤:text cti option view end 取值 scale str uicolor
// .擷取上下文,之前的上下文都是在view的drawRect方法中擷取(跟View相關聯的上下文layer上下文)
// 目前我們需要繪製圖片到新的圖片上,因此需要用到位元影像上下文
// 怎麼擷取位元影像上下文,注意位元影像內容相關的擷取方式跟layer上下文不一樣。位元影像上下文需要我們手動建立。
// 開啟一個位元影像上下文,注意位元影像上下文跟view無關聯,所以不需要在drawRect.
- (void)viewDidLoad {
[super viewDidLoad];
// 載入圖片
UIImage *image = [UIImage imageNamed:@"小黃人"];
// size:位元影像內容相關的尺寸(新圖片的尺寸)
// opaque: 不透明度 YES:不透明 NO:透明,通常我們一般都弄透明的上下文
// scale:通常不需要縮放上下文,取值為0,表示不縮放
//0.建立位元影像上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
// 1.繪製原生的圖片
[image drawAtPoint:CGPointZero];
// 2.給原生的圖片添加文字
NSString *str = @"小碼哥";
// 建立字典屬性
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
[str drawAtPoint:CGPointMake(200, 528) withAttributes:dict];
// 3.產生一張圖片給我們,從上下文中擷取圖片
UIImage *imageWater = UIGraphicsGetImageFromCurrentImageContext();
// 4.關閉上下文
UIGraphicsEndImageContext();
_imageView.image = imageWater;
}
iOS中圖片浮水印的製作