iOS中圖片浮水印的製作

來源:互聯網
上載者:User

標籤: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中圖片浮水印的製作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.