基本上,我想从用户的照片库中选择一张图片,然后应用水印,在右下角的三角形上有应用程序的名称,我已经在photoshop中用透明层制作了第二张图片。我尝试了一个函数,我记不清它的确切名字了,但它涉及到CGIMimages和蒙版。这个函数将两个图像组合在一起,但作为蒙版,它使透明层所在的图像变暗,图像本身没有合并,只是被蒙版了。我怎样才能让水印图像与另一个图像合并,形成一个UIImage,而不在屏幕上显示图像?谢谢你。
wwodge7n1#
omz提供的解决方案也适用于Swift,如下所示:
let backgroundImage = UIImage(named: "image.png")! let watermarkImage = UIImage(named: "watermark.png")! UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0) backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: backgroundImage.size.width, height: backgroundImage.size.height)) watermarkImage.draw(in: CGRect(x: backgroundImage.size.width - watermarkImage.size.width, y: backgroundImage.size.height - watermarkImage.size.height, width: watermarkImage.size.width, height: watermarkImage.size.height)) let result = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
zbsbpyhn2#
SWIFT 4
let backgroundImage = imageData! let watermarkImage = #imageLiteral(resourceName: "jodi_url_icon") UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0) backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: backgroundImage.size.width, height: backgroundImage.size.height)) watermarkImage.draw(in: CGRect(x: 10, y: 10, width: watermarkImage.size.width, height: backgroundImage.size.height - 40)) let result = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() self.imgaeView.image = result
将结果用于UIImageView,已测试。
dw1jzc5e3#
您可以使用此方法,它是非常动态的,并且您可以指定第二个图像的起始位置和图像的总大小。
-(UIImage *) addImageToImage:(UIImage *)img withImage2:(UIImage *)img2 andRect:(CGRect)cropRect withImageWidth:(int) width{ CGSize size = CGSizeMake(width,40); UIGraphicsBeginImageContext(size); CGPoint pointImg1 = CGPointMake(0,0); [img drawAtPoint:pointImg1]; CGPoint pointImg2 = cropRect.origin; [img2 drawAtPoint: pointImg2]; UIImage* result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return result; }
uemypmqf4#
SWIFT 5功能:
func addWaterMark(image: UIImage) -> UIImage { let backgroundImage = image//UIImage(named: "image.png") let watermarkImage = UIImage(named: "waterMark.png") UIGraphicsBeginImageContextWithOptions(backgroundImage.size, false, 0.0) backgroundImage.draw(in: CGRect(x: 0.0, y: 0.0, width: backgroundImage.size.width, height: backgroundImage.size.height)) watermarkImage!.draw(in: CGRect(x: backgroundImage.size.width - watermarkImage!.size.width, y: backgroundImage.size.height - watermarkImage!.size.height, width: watermarkImage!.size.width, height: watermarkImage!.size.height)) let result = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return result! }
kmynzznz5#
这很简单:
UIImage *backgroundImage = [UIImage imageNamed:@"image.png"]; UIImage *watermarkImage = [UIImage imageNamed:@"watermark.png"]; UIGraphicsBeginImageContext(backgroundImage.size); [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)]; [watermarkImage drawInRect:CGRectMake(backgroundImage.size.width - watermarkImage.size.width, backgroundImage.size.height - watermarkImage.size.height, watermarkImage.size.width, watermarkImage.size.height)]; UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
如果你想让背景和水印大小相同,那么使用下面的代码
... [backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)]; [watermarkImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)]; ...
5条答案
按热度按时间wwodge7n1#
omz提供的解决方案也适用于Swift,如下所示:
zbsbpyhn2#
SWIFT 4
将结果用于UIImageView,已测试。
dw1jzc5e3#
您可以使用此方法,它是非常动态的,并且您可以指定第二个图像的起始位置和图像的总大小。
uemypmqf4#
SWIFT 5功能:
kmynzznz5#
这很简单:
如果你想让背景和水印大小相同,那么使用下面的代码