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!