// 引用方式, 当前添加到window层的layer 实际应用时可根据具体情况添加响应的页面
+(void)addWatermark:(NSString*)watermark{
AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UIWindow *window = appDelegate.window;
if(!watermarkView){
watermarkView = [UIView new];
watermarkView.frame = window.bounds;
[window.layer addSublayer:watermarkView.layer]
watermarkView.userInteractionEnabled = NO;
}
if(watermark){
// UIImage *drawImg = [self watemarkWithText:watermark];
UIImage *drawImg = [self waterAtImage:IMG(@"europe_lotarry") waterImgae:IMG(@"europe_lotarry") rect:CGRectMake(0, 0, 110, 69)];
UIImage *rotageImg = [self rotatedImage:drawImg];
watermarkView.backgroundColor = [UIColor colorWithPatternImage:rotageImg];
}
}
// 添加文本水印
+ (UIImage*)watemarkWithText:(NSString*)watemarkText {
UIFont *font = [UIFont systemFontOfSize:10.0];
NSDictionary *attributes = @{NSFontAttributeName:font
,NSForegroundColorAttributeName:WATERMARK_COLOR
,NSKernAttributeName:@(3) //字间距
};
//字符串的size
CGSize size = [watemarkText sizeWithAttributes:attributes];
//加上向上取整 ceilf()就能解决了
CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
UIGraphicsBeginImageContextWithOptions(adjustedSize, NO, 0);
int w = adjustedSize.width;
int h = adjustedSize.height;
[watemarkText drawInRect:CGRectMake(0,0,w,h) withAttributes:attributes];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
// 添加图片水印
+ (UIImage *)waterAtImage:(UIImage *)image
waterImgae:(UIImage *)waterImage
rect:(CGRect)rect {
//开启图形上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//绘制原图片
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.width)];
//绘制水印
[waterImage drawInRect:rect];
// [waterImage drawInRect:rect blendMode:kCGBlendModeNormal alpha:0.5];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
//使水印旋转45度。
+ (UIImage*)rotatedImage:(UIImage*)image
{
CGFloat rotation = -M_PI_4;
//一行只有一个水印
CGFloat w = 0;
{
CGFloat minW = image.size.width*M_SQRT2 + 20;
CGFloat oneThirdScreen = [UIScreen mainScreen].bounds.size.width/3.0;
w = fmax(minW,oneThirdScreen);
}
//界面最多显示4行水印
CGFloat h = 0;
{
CGFloat minH = image.size.width*M_SQRT2 + 20;
CGFloat quarterScreen = [UIScreen mainScreen].bounds.size.height/4.0;
h = fmax(minH,quarterScreen);
}
CGRect destRect = CGRectMake(0, 0, w, h);
CGSize destinationSize = destRect.size;
UIGraphicsBeginImageContextWithOptions(destinationSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, destinationSize.width / 2.0f, destinationSize.height / 2.0f);
CGContextRotateCTM(context, rotation);
[image drawInRect:CGRectMake(-image.size.width / 2.0f
, -image.size.height / 2.0f
, image.size.width
, image.size.height) blendMode:kCGBlendModePlusLighter alpha:.3];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
iOS 给view添加文字或图片水印
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近要实现一个手写签名功能,要求是,在一定区域绘制文字签名,签名完成后,添加新的水印,并且将图片仅保留签字区域剪切...
- 一. 概述 在后台管理系统中, 经常会遇到各种报表开发, 其中比较有名开源的报表框架UReport2, 了解URe...