iOS图片加水印效果的实现并保存至相冊

时间:2023-03-10 07:49:40
iOS图片加水印效果的实现并保存至相冊

图片加水印效果的实现并保存至相冊

实现效果如图:

iOS图片加水印效果的实现并保存至相冊


project下载:githubproject下载链接


代码:

- (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參数说明文章