gpt4 book ai didi

ios - 在 iOS 中用另一个实际图像掩盖透明图像

转载 作者:行者123 更新时间:2023-12-01 16:36:06 25 4
gpt4 key购买 nike

我有两张图像,一张是带有一些边缘/边框的透明蒙版,另一张是实际图像。我想合并它们。

enter image description here enter image description here

我使用以下代码来屏蔽和组合图像:

 - (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

// create a bitmap graphics context the size of the image
CGFloat dim = MIN(image.size.width, image.size.height);
CGSize size = CGSizeMake(dim, dim);
UIGraphicsBeginImageContextWithOptions(size, NO, .0);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:(CGRect){ CGPointZero, size }];
[bezierPath fill];
[bezierPath addClip];
CGPoint offset = CGPointMake((dim - image.size.width) * 0.5, (dim - image.size.height) * 0.5);
[image drawInRect:(CGRect){ offset, image.size }];
UIImage *ret = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return ret;
}

结果:

enter image description here

在结果图像中,用作掩码的图像的边框缺失 .有人可以帮我吗?

最佳答案

我为 ios 编写了一个 mask 类别(好吧,它基本上是跨平台的,因为 CoreImage 无论如何都在两个平台上:

github project

核心功能归结为这一点(例如)

UIImage *person = ...
UIImage *circle = ...
UIImage *result = [person imageMaskedWith:circle];

UIImageView *redbox = [[UIImageView alloc] initWithImage:result];
redbox.backgroundColor = [UIColor redColor]; //this can be a gradient!

来自类的主要部分代码:
CGImageRef imageReference = image.CGImage;
CGImageRef maskReference = mask.CGImage;
CGRect rect = CGRectMake(0, 0, CGImageGetWidth(imageReference), CGImageGetHeight(imageReference));

// draw with Core Graphics
UIGraphicsBeginImageContext(rect.size);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, 0.0, rect.size.height);
CGContextScaleCTM(bitmap, 1.0, -1.0);

CGContextClipToMask(bitmap, rect, maskReference);
CGContextDrawImage(bitmap, rect, imageReference);

newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

关于ios - 在 iOS 中用另一个实际图像掩盖透明图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28133120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com