图片加水印效果的实现并保存至相册
实现效果如图:
工程下载:github工程下载链接
代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"pushu.jpg"];
UIImage *waterImage = [self waterMarkImage:image withText:@"朴树水印测试"];
UIImageWriteToSavedPhotosAlbum(waterImage, nil, nil, nil); //保存图片至相册
// 展示图片
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = waterImage;
[self.view addSubview:imageView];
// Do any additional setup after loading the view, typically from a nib.
}
- (UIImage *)waterMarkImage:(UIImage *)image withText:(NSString *)text {
UIGraphicsBeginImageContext(image.size);
// 在画布中绘制内容
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
// 绘制文字
[[UIColor darkGrayColor] set];
CGRect rect = CGRectMake(70, 220, 200, 60);
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:30],
NSObliquenessAttributeName:@1}; //这里设置了字体,和倾斜度,具体其他参数文章结尾有详细说明的文章链接
[text drawInRect:rect withAttributes:dic];
//在iOS7之前用下列方法比较方便
// [text drawInRect:rect withFont:[UIFont systemFontOfSize:30] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
// 从画布中得到image
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImage;
}
- (void)drawInRect:(CGRect)rect withAttributes:(NSDictionary *)attrs NS_AVAILABLE_IOS(7_0);
关于此方法,在iOS7以后应用,attrs设置参数的详细说明在如下链接文章中:
说明文章链接:attrs参数说明文章