博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ios图片加水印或文字
阅读量:6551 次
发布时间:2019-06-24

本文共 6189 字,大约阅读时间需要 20 分钟。

  hot3.png

 

ios图片加水印或文字

详情可见附件,我亲自测试了一下,发现原来的方法确实存在诸多问题,重新写了一下,大家随意取用,有问题再交流
发一张截图
6_131039_dbac75feb1fb9b3.jpg 
原帖:[url]http://hi.baidu.com/peter_wwj/item/179cbe060c570e873c42e298[/url]
1.加文字

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

- (UIImage *)imageWithLogoText:(UIImage *)img text:(NSString *)text1

{    

    /注:此为后来更改,用于显示中文。zyq,2013-5-8

    CGSize size = CGSizeMake(200, img.size.height);          //设置上下文(画布)大小

    UIGraphicsBeginImageContext(size);                       //创建一个基于位图的上下文(context),并将其设置为当前上下文

    CGContextRef contextRef = UIGraphicsGetCurrentContext(); //获取当前上下文

    CGContextTranslateCTM(contextRef, 0, img.size.height);   //画布的高度

    CGContextScaleCTM(contextRef, 1.0, -1.0);                //画布翻转

    CGContextDrawImage(contextRef, CGRectMake(0, 0, img.size.width, img.size.height), [img CGImage]);  //在上下文种画当前图片

     

    [[UIColor redColor] set];                                //上下文种的文字属性

    CGContextTranslateCTM(contextRef, 0, img.size.height);

    CGContextScaleCTM(contextRef, 1.0, -1.0);

    UIFont *font = [UIFont boldSystemFontOfSize:16];

    [text1 drawInRect:CGRectMake(0, 0, 200, 80) withFont:font];       //此处设置文字显示的位置

    UIImage *targetimg =UIGraphicsGetImageFromCurrentImageContext();  //从当前上下文种获取图片

    UIGraphicsEndImageContext();                            //移除栈顶的基于当前位图的图形上下文。

    return targetimg;

     

     

    //注:此为原来,不能显示中文。无用。

    //get image width and height

    int w = img.size.width;

    int h = img.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    //create a graphic context with CGBitmapContextCreate

    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

    CGContextSetRGBFillColor(context, 0.0, 1.0, 1.0, 1);

    char* text = (char *)[text1 cStringUsingEncoding:NSUnicodeStringEncoding];

    CGContextSelectFont(context, "Georgia", 30, kCGEncodingMacRoman);

    CGContextSetTextDrawingMode(context, kCGTextFill);

    CGContextSetRGBFillColor(context, 255, 0, 0, 1);

    CGContextShowTextAtPoint(context, w/2-strlen(text)*5, h/2, text, strlen(text));

    //Create image ref from the context

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

 

}

2.新的添加文字水印的方法一

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#pragma mark -也是文字水印

- (UIImage *) imageWithStringWaterMark:(NSString *)markString inRect:(CGRect)rect color:(UIColor *)color font:(UIFont *)font

{

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)

    {

        UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".

    }

#else

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)

    {

        UIGraphicsBeginImageContext([self size]);

    }

#endif

     

    //原图

    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];

     

    //文字颜色

    [color set];

     

    //水印文字

    [markString drawInRect:rect withFont:font];

     

    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

     

    return newPic;

}

3.还是添加文字水印方法二

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

#pragma mark -还是文字水印

- (UIImage *) imageWithStringWaterMark:(NSString *)markString atPoint:(CGPoint)point color:(UIColor *)color font:(UIFont *)font

{

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)

    {

        UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".

    }

#else

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)

    {

        UIGraphicsBeginImageContext([self size]);

    }

#endif

     

    //原图

    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];

     

    //文字颜色

    [color set];

     

    //水印文字

    [markString drawAtPoint:point withFont:font];

     

    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

     

    return newPic;

}

4.加图片

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#pragma mark - 加图片水印

-(UIImage *)imageWithLogoImage:(UIImage *)img logo:(UIImage *)logo

{

    //get image width and height

    int w = img.size.width;

    int h = img.size.height;

    int logoWidth = logo.size.width;

    int logoHeight = logo.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    //create a graphic context with CGBitmapContextCreate

    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);

    CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [logo CGImage]);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);

    CGContextRelease(context);

    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];

    //  CGContextDrawImage(contextRef, CGRectMake(100, 50, 200, 80), [smallImg CGImage]);

}

5.新的添加图片水印的方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#pragma mark -还是图片水印

- (UIImage *) imageWithWaterMask:(UIImage*)mask inRect:(CGRect)rect

{

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)

    {

        UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".

    }

#else

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)

    {

        UIGraphicsBeginImageContext([self size]);

    }

#endif

     

    //原图

    [self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];

    //水印图

    [mask drawInRect:rect];

     

    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

     

    return newPic;

}

6 .加半透明的水印

1

2

3

4

5

6

7

8

9

10

11

//加半透明的水印

-(UIImage *)imageWithTransImage:(UIImage *)useImage addtransparentImage:(UIImage *)transparentimg

{

    UIGraphicsBeginImageContext(useImage.size);

    [useImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height)];

    [transparentimg drawInRect:CGRectMake(0, useImage.size.height-transparentimg.size.height, transparentimg.size.width, transparentimg.size.height) blendMode:kCGBlendModeOverlay alpha:0.4f];

     

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return resultingImage;

}

转载于:https://my.oschina.net/SoulJa/blog/796726

你可能感兴趣的文章
Android中asset文件夹和raw文件夹区别
查看>>
第二章家庭作业 2.78
查看>>
Risc-V指令集
查看>>
Python进阶04 函数的参数对应
查看>>
C语言结构体的“继承”
查看>>
linux常用指令
查看>>
Servlet Demo
查看>>
Struts2中的<s:action>标签
查看>>
Java中取某一个范围的随机数
查看>>
一条复杂SQL实现思路
查看>>
我的友情链接
查看>>
-bash:wget command not found的解决方法
查看>>
我的个人简历
查看>>
我的友情链接
查看>>
KVM组件bug报告方法
查看>>
HTML5初学---坦克大战基础
查看>>
Solr增量更新索引
查看>>
抵制克苏恩[Lydsy2017年4月月赛]
查看>>
MySql Study Notes
查看>>
6 - laravel 基础 - 视图与模板引擎
查看>>