美文网首页
给图片添加文字、水印

给图片添加文字、水印

作者: 寂寞先森666 | 来源:发表于2019-06-23 19:37 被阅读0次

写个分类 UIImage+extension.h

//给图片添加文字

+ (UIImage*)waterImageWithImage:(UIImage*)image text:(NSString*)text textPoint:(CGPoint)point attributedString:(NSDictionary* )attributed;

//给图片添加logo

+ (UIImage*)waterImageWithImage:(UIImage*)image waterImage:(UIImage*)waterImage waterImageRect:(CGRect)rect;

// 给图片添加文字水印:

+ (UIImage*)waterImageWithImage:(UIImage*)image text:(NSString*)text textPoint:(CGPoint)point attributedString:(NSDictionary* )attributed{

    //1.开启上下文

    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);

    //2.绘制图片

    [imagedrawInRect:CGRectMake(0,0, image.size.width, image.size.height)];

    //添加水印文字

    [textdrawAtPoint:pointwithAttributes:attributed];

    //3.从上下文中获取新图片

    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();

    //4.关闭图形上下文

    UIGraphicsEndImageContext();

    //返回图片

    returnnewImage;

}

// 给图片添加图片水印

+ (UIImage*)waterImageWithImage:(UIImage*)image waterImage:(UIImage*)waterImage waterImageRect:(CGRect)rect{

    //1.获取图片

    //2.开启上下文

    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);

    //3.绘制背景图片

    [imagedrawInRect:CGRectMake(0,0, image.size.width, image.size.height)];

    //绘制水印图片到当前上下文

    [waterImagedrawInRect:rect];

    //4.从上下文中获取新图片

    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();

    //5.关闭图形上下文

    UIGraphicsEndImageContext();

    //返回图片

    returnnewImage;

}

===========================================================

在viewController 中使用

-(UIImageView*)imageview

{

    if(!_imageview) {

        _imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 400, 400)];

        UIImage * image = [UIImage imageNamed:@"20160719120225_fTS4H.thumb.700_0.png.jpeg"];

        image = [UIImagewaterImageWithImage:imagetext:@"超级大美女"textPoint:CGPointMake(100,100)attributedString:@{NSForegroundColorAttributeName:[UIColorredColor],NSFontAttributeName:[UIFontsystemFontOfSize:100]}];

        image = [UIImage waterImageWithImage:image waterImage:[UIImage imageNamed:@"logo.png"] waterImageRect:CGRectMake(50, 50, 150, 50)];

        _imageview.image= image;

    }

    return _imageview;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.viewaddSubview:self.imageview];

}

相关文章

网友评论

      本文标题:给图片添加文字、水印

      本文链接:https://www.haomeiwen.com/subject/rwvmqctx.html