前两天公司让在所有上传的图片上加上 水印,并且是,地址和时间 与公司logo 都在图片的不同位置。
一开始用iPhone6做调试,拍摄的照片加上的水印都能实现,后来突然想到,如果是不同的图片。那么获取的画布的尺寸就不同。如下:
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, W, H)];
然后用iPhoneX测试后果然,水印不知道画到哪里去了。
然后就想怎么适配所有机型的水印呢,然后想着,照片这玩意在手机上
显示不就跟相框差不多吗。不管图片多大,相框就那么大。那么灵感就来了.
为什么不试一试拿图片的宽度除以当前屏幕的宽度 来拿到一个参数呢,高类
似。 这么一想,就赶紧试一试
具体代码如下:
+ (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);
}
// 画水印
+(UIImage *)waterMarkImage:(UIImage *)image time:(NSString *)time date:(NSString *)date week:(NSString *)week
icon:(UIImage *)icon customerImage:(UIImage *)customerImage positionImage:(UIImage *)positionImage Name:(NSString *)name position:(NSString *)position{
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;
//显示时间
NSString *timeStr = time;
NSDictionary *timeAttr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:2 * Font],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
[timeStr drawInRect:[UIImage getCgrect:280 y:26 w:90 h:28 wC:wC hC:hC] withAttributes:timeAttr];
//日期
NSString *dateStr = date;
NSDictionary *dateAttr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:Font],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
[dateStr drawInRect:[UIImage getCgrect:230 y:75 + 2*correctH w:110 h:20 wC:wC hC:hC] withAttributes:dateAttr];
//星期
NSString *weekStr = week;
// weekStr = @"星期二";
NSDictionary *weekAttr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:Font],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
[weekStr drawInRect:[UIImage getCgrect:312 y:75 + 2*correctH w:60 h:20 wC:wC hC:hC] withAttributes:weekAttr];
//企业logo
UIImage *comLogo = icon;
[comLogo drawInRect:[UIImage getCgrect:300 y:113 + 3 * correctH w:40 h:40 wC:wC hC:hC]];
//客户图片
UIImage *uesrIcon = customerImage;
[uesrIcon drawInRect:[UIImage getCgrect:18 y:589.5 - 3*correctH w:11 h:16 wC:wC hC:hC]];
//客户姓名
NSString *userName = name;
NSDictionary *nameAttr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:Font],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
[userName drawInRect:[UIImage getCgrect:40 y:589.5 - 3*correctH w:200 h:40 wC:wC hC:hC] withAttributes:nameAttr];
//定位图片
UIImage *positionIcon = positionImage;
[positionIcon drawInRect:CGRectMake(18*screenW/375.0 * wC , 615*screenH/667.0 * hC , 12 * wC, 16 * hC)];
[positionIcon drawInRect:[UIImage getCgrect:18 y:615 w:12 h:16 wC:wC hC:hC]];
//定位信息
NSString *postionStr = position;
NSDictionary *positionAttr = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:Font],
NSForegroundColorAttributeName : [UIColor whiteColor]
};
[postionStr drawInRect:[UIImage getCgrect:40 y:615 w:200 h:40 wC:wC hC:hC] withAttributes:positionAttr];
UIImage *imageEnd = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return imageEnd;
}
最终实现效果如下:最终效果不错,不过我是以iPhone6的屏幕尺寸来的,
如果屏幕尺寸有变化,可以在图一中修改参数。
3ACEE38C-05B3-475C-A41C-665945AB638E.png
网友评论