iOS-为图像添加水印效果

/**    
  * 加文字随意@param logoImage 需要加文字的图片@param watemarkText 文字描述@returns 加好文字的图片 
  */
- (UIImage *)addWatemarkTextAfteriOS7_WithLogoImage:(UIImage *)logoImage watemarkText:(NSString *)watemarkText{    
    int w = logoImage.size.width;    
    int h = logoImage.size.height;    
    UIGraphicsBeginImageContext(logoImage.size);
    [[UIColor whiteColor] set];
    [logoImage drawInRect:CGRectMake(0, 0, w, h)];
    UIFont * font = [UIFont systemFontOfSize:18.0];
    [watemarkText drawInRect:CGRectMake(10, 55, 130, 80) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
/**
  * 加图片水印@param logoImage 需要加水印的logo图片@param watemarkImage 水印图片@returns 加好水印的图片 
  */
- (UIImage *)addWatemarkImageWithLogoImage:(UIImage *)logoImage watemarkImage:(UIImage *)watemarkImage logoImageRect:(CGRect)logoImageRect watemarkImageRect:(CGRect)watemarkImageRect{    
    // 创建一个graphics context来画我们的东西    
    UIGraphicsBeginImageContext(logoImageRect.size);    
    // graphics context就像一张能让我们画上任何东西的纸。我们要做的第一件事就是把person画上去    
    [logoImage drawInRect:CGRectMake(0, 0, logoImageRect.size.width, logoImageRect.size.height)];    
    // 然后在把hat画在合适的位置    
    [watemarkImage drawInRect:CGRectMake(watemarkImageRect.origin.x, watemarkImageRect.origin.y, watemarkImageRect.size.width, watemarkImageRect.size.height)];    
    // 通过下面的语句创建新的UIImage    
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();    
   // 最后,我们必须得清理并关闭这个再也不需要的context    
    UIGraphicsEndImageContext();
    return newImage;
}
/**
  * 加半透明水印@param logoImage 需要加水印的图片@param translucentWatemarkImage 水印@returns 加好水印的图片 
  */
- (UIImage *)addWatemarkImageWithLogoImage:(UIImage *)logoImage translucentWatemarkImage:(UIImage *)translucentWatemarkImage logoImageRect:(CGRect)logoImageRect translucentWatemarkImageRect:(CGRect)translucentWatemarkImageRect{    
    UIGraphicsBeginImageContext(logoImage.size);    [logoImage drawInRect:CGRectMake(0, 0, logoImage.size.width, logoImage.size.height)];       
    // 四个参数为水印的位置    
    [translucentWatemarkImage drawInRect:CGRectMake(logoImage.size.width - 110, logoImage.size.height - 25, 100, 25)];    
    UIImage * resultingImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();    
    return resultingImage;
}

你可能感兴趣的:(iOS-为图像添加水印效果)