為了避免本身辛勞做的項目被他人盜走,采用圖片添加水印,在此表現圖片的舉世無雙。加水印不是在下面添加幾個Label,而是我們把書畫到圖片上成為一個全體,上面小編給年夜家分享IOS給圖片添加水印(兩種方法)。
供給一個辦法,此辦法只須要傳遞一個要加水印的圖片和水印的內容就到達後果。
第一種方法:
-(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(, , w, h)];
NSDictionary *attr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:], //設置字體
NSForegroundColorAttributeName : [UIColor redColor] //設置字體色彩
};
[mark draWinRect:CGRectMake(, , , ) withAttributes:attr]; //左上角
[mark draWinRect:CGRectMake(w - , , , ) withAttributes:attr]; //右上角
[mark drawInRect:CGRectMake(w - , h - - , , ) withAttributes:attr]; //右下角
[mark drawInRect:CGRectMake(, h - - , , ) withAttributes:attr]; //左下角
UIImage *aimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return aimg;
}
第二種方法:用drawInRect很便利,圖片、文字都可以加
// 畫水印
- (UIImage *) imageWithWaterMask:(UIImage*)mask inRect:(CGRect)rect
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)
{
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
}
#else
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)
{
UIGraphicsBeginImageContext([self size]);
}
#endif
//原圖
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
//水印圖
[mask drawInRect:rect];
UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newPic;
}
以上論述用兩種方法完成IOS給圖片添加水印,須要的同伙可以來參考下,願望年夜家可以或許愛好。
【IOS給圖片添加水印(兩種方法)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!