IOS to add watermarks to photos, text

Source: Internet
Author: User


+ (Instancetype) Waterimagewithtext: (UIImage *) img textlogocolor: (NSString *) text1{
Note: This is later changed to display Chinese. Zyq,2013-5-8
Cgsize size = Cgsizemake (Img.size.width, img.size.height); Setting the context (canvas) size
Uigraphicsbeginimagecontext (size); Creates a bitmap-based context, and sets it to the current context
Cgcontextref contextref = Uigraphicsgetcurrentcontext (); Gets the current context
CGCONTEXTTRANSLATECTM (contextref, 0, img.size.height); The height of the canvas
CGCONTEXTSCALECTM (Contextref, 1.0,-1.0); Canvas Flip
Cgcontextdrawimage (Contextref, cgrectmake (0, 0, Img.size.width, img.size.height), [img Cgimage]); Painting the current picture in context
[[Uicolor Whitecolor] set]; Text attributes for context types
CGCONTEXTTRANSLATECTM (contextref, 0, img.size.height);
CGCONTEXTSCALECTM (Contextref, 1.0,-1.0);
Uifont *font = [Uifont boldsystemfontofsize:30];
[Text1 drawinrect:cgrectmake (0, 0, () Withfont:font]; Where to set the text display here
[Text1 Drawinrect:cgrectmake (img.size.height-40, img.size.width-80, max) withattributes:[nsdictionary Dictionarywithobjectsandkeys:font,nsfontattributename, Nil]];
[Text1 Drawinrect:cgrectmake (img.size.height-40, img.size.width-80, Max) Withfont:font];
UIImage *targetimg =uigraphicsgetimagefromcurrentimagecontext (); Get a picture from the current context type
Uigraphicsendimagecontext (); Removes the top of the stack based on the current bitmap's graphics context.
return targetimg;

}

+ (Instancetype) Waterimagewithimage: (UIImage *) Bgimage Logo: (NSString *) logo{

Uigraphicsbeginimagecontextwithoptions (Bgimage.size, NO, 0.0);//1. Create a bitmap-based context

[Bgimage drawinrect:cgrectmake (0, 0, bgImage.size.width, bgImage.size.height)];//2. Painting background

UIImage *waterimage = [UIImage imagenamed:logo];//3. Draw the watermark in the lower left corner

CGFloat scale = 1;

CGFloat margin = 5;

CGFloat WATERW = waterImage.size.width * scale;

CGFloat waterh = waterImage.size.height * scale;

CGFloat Waterx = 0;

CGFloat watery = bgimage.size.height-waterh-margin;

[Waterimage Drawinrect:cgrectmake (Waterx, watery, waterw, Waterh)];


UIImage *newimage = Uigraphicsgetimagefromcurrentimagecontext ();//4. Get the completed UIImage object from the context

Uigraphicsendimagecontext ();//5. End Context,

return newimage;
}
+ (Instancetype) Waterimagewithtext: (UIImage *) img Textlogo: (NSString *) text1{

Cgsize Size=cgsizemake (Img.size.width, img.size.height);

Uigraphicsbeginimagecontext (size);

Cgcontextref Contextref=uigraphicsgetcurrentcontext ();//Context

Transformation matrix

CGCONTEXTTRANSLATECTM (contextref, 0, img.size.height);//height of the canvas. Move function

CGCONTEXTSCALECTM (Contextref, 1.0,-1.0);//Zoom function

Cgcontextdrawimage (Contextref,cgrectmake (0,0,img.size.width,img.size.height), [img Cgimage]);//painting the current picture in context

[[Uicolor Redcolor] set]; Text attributes for context types

CGCONTEXTTRANSLATECTM (contextref, 0, img.size.height);

CGCONTEXTSCALECTM (Contextref, 1.0,-1.0);

Uifont *font = [Uifont boldsystemfontofsize:30];

[Text1 Drawinrect:cgrectmake (img.size.height-40, img.size.width-80, max) withattributes:[nsdictionary Dictionarywithobjectsandkeys:font,nsfontattributename, Nil]];

UIImage *targetimg =uigraphicsgetimagefromcurrentimagecontext ();//Get a picture from the current context type

Uigraphicsendimagecontext ();

return targetimg;
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

IOS to add watermarks to photos, text

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.