码迷,mamicode.com
首页 > 其他好文 > 详细

图片右下角显示水印

时间:2016-01-08 13:21:21      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

1.首先创建一个Image类,.h文件复制

/**

 *  打水印

 *

 *  @param bg   背景图片

 *  @param logo 右下角的水印图片

 */

+ (UIImage *)waterImageWithBg:(NSString *)bg logo:(NSString *)logo;

2.在.m文件中实现方法;

+ (UIImage *)waterImageWithBg:(NSString *)bg logo:(NSString *)logo
{
    UIImage *bgImage = [UIImage imageNamed:bg];
    
    // 1.创建一个基于位图的上下文(开启一个基于位图的上下文)
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
    
    // 2.画背景
    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
    
    // 3.画右下角的水印
    UIImage *waterImage = [UIImage imageNamed:logo];
    CGFloat scale = 0.2;
    CGFloat margin = 5;
    CGFloat waterW = waterImage.size.width * scale;
    CGFloat waterH = waterImage.size.height * scale;
    CGFloat waterX = bgImage.size.width - waterW - margin;
    CGFloat waterY = bgImage.size.height - waterH - margin;
    [waterImage drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
    
    // 4.从上下文中取得制作完毕的UIImage对象
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 5.结束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}

然后在要调用的controller中调用此方法 

UIImage *newImage = [MJUiimage waterImageWithBg:@"scene" logo:@"logo"];
    // 2.显示图片
    self.iconView.image = newImage;

 

图片右下角显示水印

标签:

原文地址:http://www.cnblogs.com/h-tao/p/5112685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!