Main.storyboard
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13D65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="vXZ-lx-hvc"> <dependencies> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/> </dependencies> <scenes> <!--View Controller--> <scene sceneID="ufC-wZ-h7g"> <objects> <viewController id="vXZ-lx-hvc" customClass="LWTViewController" sceneMemberID="viewController"> <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS"> <rect key="frame" x="0.0" y="0.0" width="320" height="480"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <subviews> <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="tF8-9P-CEF"> <rect key="frame" x="0.0" y="0.0" width="320" height="480"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> </imageView> </subviews> <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> </view> <connections> <outlet property="imageView" destination="tF8-9P-CEF" id="Hph-ej-7S9"/> </connections> </viewController> <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/> </objects> </scene> </scenes> <simulatedMetricsContainer key="defaultSimulatedMetrics"> <simulatedStatusBarMetrics key="statusBar"/> <simulatedOrientationMetrics key="orientation"/> <simulatedScreenMetrics key="destination"/> </simulatedMetricsContainer> </document>
LWTViewController.h
#import <UIKit/UIKit.h> @interface LWTViewController : UIViewController @end
LWTViewController.m
1 // 2 // LWTViewController.m 3 // Quartz2D练习 -- 绘制水印的image分类 4 // 5 // Created by apple on 14-6-10. 6 // Copyright (c) 2014年 lwt. All rights reserved. 7 // 8 9 #import "LWTViewController.h" 10 #import "UIImage+WaterStain.h" 11 12 @interface LWTViewController () 13 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 14 15 @end 16 17 @implementation LWTViewController 18 19 - (void)viewDidLoad 20 { 21 [super viewDidLoad]; 22 // Do any additional setup after loading the view, typically from a nib. 23 24 UIImage *newImage = [UIImage imageWithImageName:@"scene" waterStainIconName:@"logo"]; 25 UIImage *newImage1 = [UIImage imageWithImageName:@"scene" waterStainNSString:@"我是水印" CGSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:[UIFont systemFontOfSize:7.0] padding:5]; 26 27 /* 28 // 测试图片效果 29 self.imageView.image = newImage; 30 self.imageView.frame = CGRectMake(50, 50, imageX, imageY); 31 */ 32 // 保存图片 33 // 将图片传承二进制数据 34 NSData *data = UIImagePNGRepresentation(newImage); 35 NSData *data1 = UIImagePNGRepresentation(newImage1); 36 37 // 保存图片的地址 38 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"waterStain.png"]; 39 NSString *path1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"waterStain1.png"]; 40 41 NSLog(@"%@",path); 42 43 // 保存图片 44 [data writeToFile:path atomically:YES]; 45 [data1 writeToFile:path1 atomically:YES]; 46 47 } 48 49 @end
UIImage+WaterStain.h
// // UIImage+WaterStain.h // Quartz2D练习 -- 绘制水印的image分类 // // Created by apple on 14-6-10. // Copyright (c) 2014年 lwt. All rights reserved. // #import <UIKit/UIKit.h> @interface UIImage (WaterStain) /** * 给图片添加左上角图片水印 * * @param imageName 要添加水印的图片 * @param iconName 水印图片 * * @return 添加了水印的图片 */ + (UIImage *)imageWithImageName:(NSString *)imageName waterStainIconName:(NSString *)iconName; /** * 给图片右下角添加文字图片水印 * * @param imageName 要添加水印的图片 * @param waterStr 添加的水印文字 * @param maxSize 文字最大范围 * @param textFont 字体大小 * @param padding 文字的内边距 * * @return 添加了水印的图片 */ + (UIImage *)imageWithImageName:(NSString *)imageName waterStainNSString: (NSString *)waterStr CGSize:(CGSize)maxSize font: (UIFont *)textFont padding: (CGFloat)padding; @end
UIImage+WaterStain.m
// // UIImage+WaterStain.m // Quartz2D练习 -- 绘制水印的image分类 // // Created by apple on 14-6-10. // Copyright (c) 2014年 lwt. All rights reserved. // #import "UIImage+WaterStain.h" #import "NSString+Extension.h" @implementation UIImage (WaterStain) + (UIImage *)imageWithImageName:(NSString *)imageName waterStainIconName:(NSString *)iconName { UIImage *image = [UIImage imageNamed:imageName]; CGFloat imageX = image.size.width; CGFloat imageY = image.size.height; // 创建bigmap图形上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); // 绘制图片 [image drawInRect:CGRectMake(0, 0, imageX, imageY)]; CGFloat margin = 10; // 绘制图片水印 UIImage *icon = [UIImage imageNamed:iconName]; CGFloat iconX = imageX - icon.size.width - margin; CGFloat iconY = margin; CGFloat iconW = icon.size.width; CGFloat iconH = icon.size.height; // 绘制水印图片 [icon drawInRect:CGRectMake(iconX, iconY, iconW, iconH)]; // 获取绘制的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); return newImage; } + (UIImage *)imageWithImageName:(NSString *)imageName waterStainNSString:(NSString *)waterStr CGSize:(CGSize)maxSize font:(UIFont *)textFont padding:(CGFloat)padding { UIImage *image = [UIImage imageNamed:imageName]; CGFloat imageX = image.size.width; CGFloat imageY = image.size.height; // 创建bigmap图形上下文 UIGraphicsBeginImageContextWithOptions(image.size, NO, 0); // 绘制图片 [image drawInRect:CGRectMake(0, 0, imageX, imageY)]; // 绘制文字水印 NSString *str = waterStr; // 获取文字的尺寸 CGSize size = [str sizeWithFont:textFont andMaxSize:maxSize]; // 获取文字位置 CGFloat strX = imageX - size.width - padding; CGFloat strY = imageY - size.height - padding; NSMutableDictionary *dicts = [NSMutableDictionary dictionary]; dicts[NSForegroundColorAttributeName] = [UIColor orangeColor]; dicts[NSFontAttributeName] = textFont; // 绘制水印文字 [str drawAtPoint:CGPointMake(strX, strY) withAttributes:dicts]; // 获取绘制的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); return newImage; } @end
NSString+Extension.h
#import <Foundation/Foundation.h> @interface NSString (Extension) - (CGSize)sizeWithFont:(UIFont *)font andMaxSize: (CGSize)maxSize; @end
NSString+Extension.m
#import "NSString+Extension.h" @implementation NSString (Extension) - (CGSize)sizeWithFont:(UIFont *)font andMaxSize:(CGSize)maxSize { NSDictionary *dict = @{NSFontAttributeName : font}; CGSize size = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; return size; } @end