ios -bitmap上下文生成图片 生成水印

时间:2024-12-27 13:35:38
- (void)test
{
// 0. 加载背景图片
UIImage *image = [UIImage imageNamed:@"psb"]; // 1.创建bitmap上下文
// 执行完这一行在内存中就相遇创建了一个UIImage
UIGraphicsBeginImageContextWithOptions(image.size, NO, ); // 2.绘图图片
// 2.1绘制背景图片
[image drawAtPoint:CGPointMake(, )]; // 2.2绘制水印 UIImage *logImage = [UIImage imageNamed:@"logo1"]; CGFloat margin = ;
CGFloat logY = margin;
CGFloat logX = image.size.width - margin - logImage.size.width;
[logImage drawAtPoint:CGPointMake(logX, logY)]; // 2.3绘制文字水印
NSString *str = @"黑马程序员";
[str drawAtPoint:CGPointMake(, ) withAttributes:nil]; // 3.获得bitmap上下文中绘制号的图片
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 4.将图片写到文件中
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"aaa.png"];
NSLog(@"%@", path); NSData *data = UIImagePNGRepresentation(newImage);
[data writeToFile:path atomically:YES];
}