给图片添加文字、水印

写个分类 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];

}


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容