在做項(xiàng)目的時(shí)候我們需要將拍攝的照片做上標(biāo)記防止圖片被他人盜用,所以這就需要在照片的上面加上水印,以表示此照片的獨(dú)一無(wú)二。
加水印不是要在上面添加上幾個(gè)Label,而是我們要把字畫(huà)到圖片上成為一個(gè)整體。
提供一個(gè)方法 此方法只需要傳遞一個(gè)要加水印的圖片和水印的內(nèi)容就達(dá)到效果
-(UIImage *)watermarkImage:(UIImage *)img withName:(NSString *)name
{
NSString* mark = name;
int w = img.size.width;
int h = img.size.height;
UIGraphicsBeginImageContext(img.size);
[img drawInRect:CGRectMake(0, 0, w, h)];
NSDictionary *attr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:20], //設(shè)置字體
NSForegroundColorAttributeName : [UIColor redColor] //設(shè)置字體顏色
};
[mark drawInRect:CGRectMake(0, 10, 80, 32) withAttributes:attr]; //左上角
[mark drawInRect:CGRectMake(w - 80, 10, 80, 32) withAttributes:attr]; //右上角
[mark drawInRect:CGRectMake(w - 80, h - 32 - 10, 80, 32) withAttributes:attr]; //右下角
[mark drawInRect:CGRectMake(0, h - 32 - 10, 80, 32) withAttributes:attr]; //左下角
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aimg;
}