需求锁屏+home键截图、录屏需要有水印
设计思路:创建一个最上层的隐形图层,在截图或者触发录屏时候显示该图层
水印很好实现 上下文+文本就能在图片上打水印了
UIGraphicsBeginImageContext(image.size);
[imagedrawInRect:CGRectMake(0,0, image.size.width, image.size.height)];
CGSizetextSize = [textsizeWithAttributes:attributes];
[textdrawInRect:CGRectMake(point.x, point.y, textSize.width, textSize.height)withAttributes:attributes];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIScreenCapturedDidChangeNotification iOS11以后有录屏功能 当录屏状态发生变化会收到该通知
UIScreen * sc = [UIScreen mainScreen];
if(@available(iOS11.0,*)) {
if(sc.isCaptured) {
[[WatermarkTool shareManager] showWaterMark];
}else{
[[WatermarkTool shareManager] hideWaterMark];
}
}else{
//ios 11之前处理 未知
}
本来以为能拦截到截屏的事件,但是只有UIApplicationUserDidTakeScreenshotNotification这个通知,此时截的图已经进入到了相册。
最终用的方案是将水印透明度弄浅保证肉眼看不出,再图片颜色深化进行水印显现。
//通过上下文 取出每个像素点
for(intj =0; j < inputHeight; j++) {
for(inti =0; i < inputWidth; i++) {
@autoreleasepool {
UInt32*currentPixel = inputPixels + (j * inputWidth) + i;
UInt32color = *currentPixel;
UInt32thisR,thisG,thisB,thisA;
thisR =R(color);
thisG =G(color);
thisB =B(color);
thisA =A(color);
UInt32newR,newG,newB;
newR = [selfmixedCalculation:thisR];
newG = [selfmixedCalculation:thisG];
newB = [selfmixedCalculation:thisB];
*currentPixel =RGBAMake(newR,
newG,
newB,
thisA);
}
}
}
//颜色加深
- (int)mixedCalculation:(int)originValue {// 结果色 = 基色 -(基色反相×混合色反相)/ 混合色
intmixValue =20;
intresultValue =0;
if(mixValue ==0) {
resultValue =0;
}else{
resultValue = originValue - (255- originValue) * (255- mixValue) / mixValue;
}
if(resultValue <0) {
resultValue =0;
}
returnresultValue;
}