//
// ViewController.m
// WeaterImage
//
// Created by 韩小杰 on 2019/5/24.
// Copyright © 2019 韩小杰. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView *imgView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imgView = [[UIImageView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.imgView];
UIImage *img = [UIImage imageNamed:@"logo"];
UIImage *logo = [UIImage imageNamed:@"需要添加水印的图片"];
self.imgView.image = [self waterMarkImage:img login:logo title:@"@王木木"];
UIImageWriteToSavedPhotosAlbum(self.imgView.image, self, @selector(completedWithImage:error:context:), NULL);
}
-(UIImage *)waterMarkImage:(UIImage *)image login:(UIImage *)login title:(NSString *)title{
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
CGFloat W = image.size.width;
CGFloat H = image.size.height;
CGFloat correctH = 0;
if(W >= H){
correctH = 15;
}else{
correctH = 0;
}
CGFloat Font = 13;
CGFloat wC = W/screenW;
CGFloat hC = H/screenH;
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, W, H)];
Font = Font * wC;
CGRect postionIconF = [self getCgrect:18 y:615 w:16 h:20 wC:wC hC:hC];
CGRect postionStrF = [self getCgrect:40 y:615 w:200 h:40 wC:wC hC:hC];
CGSize size = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, postionStrF.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:Font]} context:nil].size;
CGFloat screenHeight = image.size.height - postionIconF.size.height;
CGFloat postionIconFx = (image.size.width - postionIconF.size.width - size.width - 10) /2;
postionIconF = CGRectMake(postionIconFx, screenHeight, postionIconF.size.width, postionIconF.size.height);
CGFloat postionStrFx = postionIconF.origin.x + postionIconF.size.width + 10;
postionStrF = CGRectMake(postionStrFx, screenHeight, size.width + 20, postionStrF.size.height);
UIImage *loginIcon = login;
[loginIcon drawInRect:CGRectMake(postionIconF.origin.x, postionIconF.origin.y, postionIconF.size.width, postionIconF.size.width)];
NSString *titleStr = title;
NSDictionary *positionAttr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:Font],
NSForegroundColorAttributeName : [UIColor redColor]
};
[titleStr drawInRect:postionStrF withAttributes:positionAttr];
UIImage *imageEnd = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageEnd;
}
- (CGRect)getCgrect:(CGFloat)x y:(CGFloat)y w:(CGFloat)w h:(CGFloat)h wC:(CGFloat)wC hC:(CGFloat)hC{
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
return CGRectMake(x * screenW/375.0 * wC, y*screenH/667.0 * hC, w * wC, h * wC);
}
- (void)completedWithImage:(UIImage *)image error:(NSError *)error context:(void *)context {
if (error) {
NSLog(@"failure");
} else {
NSLog(@"successful");
}
}
@end
图片添加logo和文字水印
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 公司新闻视频直播需要添加实时文字和gif水印,在网上看了下大部分都是基于GPUImage来处理的,发现大部分添加水...
- 话不多说 上图 在Activity中获取到ImageView对象,并且获取Bitmap对象,对Bitmap进行ca...