iOS development example of adding Chinese watermarks to images

Source: Internet
Author: User

Look at an effect chart first.

It seems simple, but it's not. Next I will share with you in this demo I encountered some of the pits, I believe that the same as I iOS development beginners will certainly help.

First, one of my first ways was to use cgbitmapcontextcreate to create the context and then use Cgcontextselectfont to set the font and font size, and finally to use Cgcontextshowtextatpoint The watermark is synthesized into the context, and then the cgbitmapcontextcreateimage is called to generate a new picture. All this seems logical, the final effect is also true, but when I input Chinese, the problem arises, needless to say that everyone should have just arrived, a headache garbled appearance. So I began to change the code, to change the font, all kinds of changes, and ultimately no fruit. But began to search all kinds of data, finally let me find the original Cgcontextshowtextatpoint method does not support Chinese output. At this point, the thought has been done, but had to start again!

Since the above method can not get the result I want, I have to find another way. I have to say that today's internet is really very powerful, and soon I was searching for another way to achieve it, so I started to move hands again. This is accomplished by first using Uigraphicsbeginimagecontext (cgsize) to create the picture context, and then:

The code is as follows Copy Code


[UIImage Drawinrect:cgrect];

Draw the main picture, and then:

The code is as follows Copy Code
[NSString Drawinrect:cgrect Withfont:uifont];


Draw the watermark text, which you can also use here:

The code is as follows Copy Code

[[Uicolor Redcolor] set];

Set watermark text color, if you want to draw a picture watermark, this is the way to draw the main diagram before the effective. Next use:

The code is as follows Copy Code

Objective-c
UIImage *aimg = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();

Generates a new picture and closes the context. Emperor not negative, this time finally everything is normal. Here's the main code:

The code is as follows Copy Code

Objective-c
-(UIImage *) AddText: (uiimage *) img Text: (NSString *) Mark {
int w = img.size.width;
int h = img.size.height;
Uigraphicsbeginimagecontext (img.size);
[[Uicolor Redcolor] set];
[img drawinrect:cgrectmake (0, 0, W, h)];
[Mark Drawinrect:cgrectmake, 130, Withfont:[uifont systemfontofsize:18]];
UIImage *aimg = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
return aimg;

}

Well, this article to come to an ending, welcome you to comment, a lot of exchanges!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.