IOS圖片上添加浮水印文字(微博上圖片上得浮水印文字)

來源:互聯網
上載者:User

標籤:ios   圖片   微博   繪圖   


建一個類目(不知道怎麼建的可以去百度一下) UIImage的類目

類目的.h檔案  檔案名稱為UIImage+watermark

#import <UIKit/UIKit.h>


@interface UIImage (watermark)

//定義一個方法

- (UIImage *)watermarkImage:(NSString *)text;

@end


.m檔案的實現

#import "UIImage+watermark.h"


@implementation UIImage (watermark)

- (UIImage *)watermarkImage:(NSString *)text{


    //1.擷取上下文

    UIGraphicsBeginImageContext(self.size);

    

    //2.繪製圖片

    [selfdrawInRect:CGRectMake(0,0, self.size.width,self.size.height)];

    

    //3.繪製浮水印文字

   CGRect rect = CGRectMake(0,self.size.height-20,self.size.width,20);

    

    NSMutableParagraphStyle *style = [[NSMutableParagraphStyledefaultParagraphStyle]mutableCopy];

    style.alignment =NSTextAlignmentCenter;

    //文字的屬性

   NSDictionary *dic = @{

                         NSFontAttributeName:[UIFontsystemFontOfSize:13],

                         NSParagraphStyleAttributeName:style,

                          NSForegroundColorAttributeName:[UIColorwhiteColor]

                         };

    //將文字繪製上去

    [text drawInRect:rectwithAttributes:dic];


    //4.擷取繪製到得圖片

    UIImage *watermarkImage =UIGraphicsGetImageFromCurrentImageContext();

    

    //5.結束圖片的繪製

    UIGraphicsEndImageContext();

    

   return watermarkImage;

}

@end



然後再viewController 的視圖上添加一個UIImageView 匯入類目(UIImage+watermark)檔案

#import "ViewController.h"

#import "UIImage+watermark.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];


    

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


//調用類目的方法實現功能

   UIImage *img = [image watermarkImage:@"@大白"];

    

   UIImageView *imageView = [[UIImageViewalloc] initWithImage:img];

//    imageView.frame = self.view.bounds;

    

    [self.viewaddSubview:imageView];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




IOS圖片上添加浮水印文字(微博片上得浮水印文字)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.