UIImage *bgImage = [UIImage imageNamed:@"bg"]; //上下文 : 基于位图(bitmap),所有的东西需要绘制到一张新的图片上去 //1.创建一个基于位图的上下文(开启一个基于位图的上下文) //size : 新图片的此处 //opaque : 不透明度 //创建一张新的bitmap,也就是新的UIImage对象 UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0); //2.画背景 [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)]; //3.画右下角水印 UIImage *watermarkImage = [UIImage imageNamed:@"watermark"]; CGFloat scale = 0.2; CGFloat margin = 5; CGFloat watermarkW = watermarkImage.size.width * scale; CGFloat watermarkH = watermarkImage.size.height * scale; CGFloat watermarkX = bgImage.size.width - watermarkW - margin; CGFloat watermarkY = bgImage.size.height - watermarkH - margin; [watermarkImage drawInRect:CGRectMake(watermarkX, watermarkY, watermarkW, watermarkH)]; NSLog(@"%g",watermarkY); //4.从上下文中取得制作完毕的UIImange对象 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); //5.结束上下文 UIGraphicsEndImageContext(); //6.将image对象压缩为PNG格式的二进制对象. NSData *data = UIImagePNGRepresentation(newImage); //7写入文件 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingString:@"new.png"]; [data writeToFile:path atomically:YES]; NSLog(@"%@",path);
时间: 2024-10-14 04:19:16