安全专题

iOS 给网络图片动态添加水印

2018-11-29  本文已影响0人  心在前方

问题简述

关于如何给图片添加水印,我相信网上有很多代码可以实现,不过却并不怎么完善。添加了水印之后可能出现 设置的水印大小,位置等都出现偏差的情况。

@interface UIImage (Water)
+ (UIImage *)jq_WaterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary * )attributed;
@end  
@implementation UIImage (Water)

+ (UIImage *)jq_WaterImageWithImage:(UIImage *)image text:(NSString *)text textPoint:(CGPoint)point attributedString:(NSDictionary *)attributed{
    //1.开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.绘制图片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //添加水印文字
    [text drawAtPoint:point withAttributes:attributed];
    //3.从上下文中获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4.关闭图形上下文
    UIGraphicsEndImageContext();
    //返回图片
    return newImage;
}
@end
  1. 在VC中引入分类,并调用方法
//设置水印
//对文字水印可以使用dic对其更改样式
NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:20], NSForegroundColorAttributeName :[UIColor whiteColor],};
UIImage *image  = [UIImage ImageNamed:@"xxxx"];

UIImage *newImage = [UIImage jq_WaterImageWithImage:image text:[NSString stringWithFormat:@"可能会发生问题的水印"] textPoint:(CGPointMake(width - Screen_Ratio(180), height - Screen_Ratio(30))) attributedString:dic];
  1. 在Image的分类中,添加以下一个方法,对image的size进行调整(image.size属性是readOnly)
- (UIImage *)imageByScalingToSize:(CGSize)targetSize;
- (UIImage *)imageByScalingToSize:(CGSize)targetSize
{
    UIImage *sourceImage = self;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) ==NO) {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if (widthFactor < heightFactor)
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        // center the image
        if (widthFactor < heightFactor) {
            
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        } else if (widthFactor > heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    // this is actually the interesting part:
    UIGraphicsBeginImageContext(targetSize);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    return newImage ;
}
  1. 在设置水印之前,先对image的大小进行处理
UIImage *image = [UIImage imageNamed:@"xxx"];
CGFloat scale = image.size.height / image.size.width;
// ScreenWidth 为屏幕宽度; 30 为图片与屏幕两边的间距
CGFloat width = ScreenWidth - 30;
CGFloat height = width * scale;
image = [image imageByScalingToSize:CGSizeMake(width, height)];
//设置水印
//对文字水印可以使用dic对其更改样式
NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:20], NSForegroundColorAttributeName :[UIColor whiteColor],};

UIImage *newImage = [UIImage jq_WaterImageWithImage:image text:[NSString stringWithFormat:@"这是你想要的水印吗?"] textPoint:(CGPointMake(width - Screen_Ratio(180), height - Screen_Ratio(30))) attributedString:dic];
//如果是网络图片,使用SDwebImage框架下载图片后,对image进行设置
SDWebImageManager *manager = [SDWebImageManager sharedManager] ;
[manager downloadImageWithURL:imageURL options:0 progress:^(NSInteger   receivedSize, NSInteger expectedSize) { 
      // progression tracking code
 }  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType,   BOOL finished, NSURL *imageURL) { 
   if (image) { 
       // do something with image
      CGFloat scale = image.size.height / image.size.width;
      // ScreenWidth 为屏幕宽度; 30 为图片与屏幕两边的间距
      CGFloat width = ScreenWidth - 30;
      CGFloat height = width * scale;
      image = [image imageByScalingToSize:CGSizeMake(width, height)];
      //设置水印
      //对文字水印可以使用dic对其更改样式
      NSDictionary *dic = @{NSFontAttributeName : [UIFont systemFontOfSize:20],             
      NSForegroundColorAttributeName :[UIColor whiteColor],};

      UIImage *newImage = [UIImage jq_WaterImageWithImage:image text:[NSString stringWithFormat:@"这是你想要的水印吗?"] textPoint:(CGPointMake(width - Screen_Ratio(180), height - Screen_Ratio(30))) attributedString:dic];
   }
 }];

实际效果图

给图片添加图片水印的方法,大致差不多。

上一篇 下一篇

猜你喜欢

热点阅读