创建一个category文件
.h
#import <UIKit/UIKit.h>
@interface UIImage (ImageWaterPrint)
-(UIImage *)imageWater:(UIImage *)imageLogo waterString:(NSString*)waterString;
@end
.m
#import "UIImage+ImageWaterPrint.h"
@implementation UIImage (ImageWaterPrint)
-(UIImage *)imageWater:(UIImage *)imageLogo waterString:(NSString*)waterString{
UIGraphicsBeginImageContext(self.size);
//原始图片渲染
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
CGFloat waterX = 633;
CGFloat waterY = 461;
CGFloat waterW = 16;
CGFloat waterH = 16;
//logo的渲染
[imageLogo drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];
//渲染文字
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle]mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[UIColor redColor]};
[waterString drawInRect:CGRectMake(0, 0, 300, 60) withAttributes:dic];
UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); // 此处注意要最后写入关闭绘制
return imageNew;
}
@end
ViewController中
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self imageWaterPrint];
}
-(void)imageWaterPrint{
UIImage *image = [UIImage imageNamed:@"1"];
UIImage *imageLogo =[UIImage imageNamed:@"2"];
UIImage *imageNew =[image imageWater:imageLogo waterString:@"哲"];
UIImageWriteToSavedPhotosAlbum(imageNew, nil, nil, nil); // 将图片保存到手机相册中
}
@end
添加 <key>NSPhotoLibraryUsageDescription</key> <string>App需要您的同意,才能访问相册</string> iOS 10如果访问必须添加否则闪退