Add watermark text to the iOS image (watermark text on the image on Weibo)

Source: Internet
Author: User


Build a category (do not know how to build can go to Baidu a bit) uiimage category

class purpose. h file file name is Uiimage+watermark

#import <UIKit/UIKit.h>


@interface UIImage (Watermark)

Define a method

-(UIImage *) Watermarkimage: (nsstring *) text;

@end


Implementation of the. m file

#import "Uiimage+watermark.h"


@implementation UIImage (Watermark)

-(UIImage *) Watermarkimage: (nsstring *) text{


//1. Get Context

uigraphicsbeginimagecontext(self. size );

//2. Drawing Pictures

[selfdrawinrect:cgrectmake(0,0, self. Size. width,self. size. height)];

//3. Draw Watermark Text

cgrect rect = cgrectmake(0,self. Size. height-self. size. width,a);

nsmutableparagraphstyle *style = [[nsmutableparagraphstyleDefaultparagraphstyle ]mutablecopy];

style. Alignment = Nstextalignmentcenter;

// properties of text

nsdictionary *dic = @{

nsfontattributename: [uifontsystemfontofsize:],

nsparagraphstyleattributename: Style,

nsforegroundcolorattributename: [uicolorwhitecolor]

};

// draw the text up

[Text drawinrect: Rectwithattributes:d ic];


//4. get the picture you're drawing

UIImage *watermarkimage =uigraphicsgetimagefromcurrentimagecontext();

//5. end drawing of a picture

uigraphicsendimagecontext();

return watermarkimage;

}

@end



Then add a Uiimageview import class (Uiimage+watermark) file to the Viewcontroller view

#import "ViewController.h"

#import "Uiimage+watermark.h"

@interface viewcontroller ()


@end


@implementation Viewcontroller


-(void) Viewdidload {

[superviewdidload];


UIImage *image = [UIImageimagenamed:@ "baymax.jpg"];


Invoke class Intent method implementation function

UIImage *img = [Image watermarkimage:@ "@ white "];

uiimageview *imageview = [[uiimageviewalloc] initwithimage: img];

Imageview.frame = Self.view.bounds;

[Self. View addsubview: ImageView];

}


-(void) didreceivememorywarning {

[superdidreceivememorywarning];

//Dispose of any resources, can be recreated.

}


@end




Add watermark Text to iOS images (watermark text on Weibo)

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.