Add text and watermarks

Source: Internet
Author: User

1. Add text

-(UIImage *) AddText: (UIImage *) img Text: (NSString *) Text1
{
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:nsasciistringencoding];
Cgcontextselectfont (Context, "Georgia", Kcgencodingmacroman);
Cgcontextsettextdrawingmode (context, Kcgtextfill);
Cgcontextsetrgbfillcolor (context, 255, 0, 0, 1);
Cgcontextshowtextatpoint (Context, W/2-strlen (text), H/2, text, strlen (text));
Create image ref from the context
Cgimageref imagemasked = cgbitmapcontextcreateimage (context);
Cgcontextrelease (context);
Cgcolorspacerelease (ColorSpace);
return [UIImage imagewithcgimage:imagemasked];
}

2. Add picture
-(UIImage *) Addimagelogo: (UIImage *) img Text: (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 (), [smallimg cgimage]);
}

3. Add a translucent watermark
-(UIImage *) AddImage: (UIImage *) useimage addImage1: (UIImage *) addImage1
{
Uigraphicsbeginimagecontext (useimage.size);
[Useimage drawinrect:cgrectmake (0, 0, UseImage.size.width, useImage.size.height)];
[AddImage1 drawinrect:cgrectmake (0, Useimage.size.height-addimage1.size.height, AddImage1.size.width, AddImage1.size.height)];
UIImage *resultingimage = Uigraphicsgetimagefromcurrentimagecontext ();
Uigraphicsendimagecontext ();
return resultingimage;}

Add text and watermarks

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.