+ (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