gpt4 book ai didi

iphone - 如何在图像 (*.png) 周围绘制轮廓/描边 - 内部示例

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:46 26 4
gpt4 key购买 nike

我尝试了很多很多方法在图像周围画一个黑色轮廓。
这是我想要的结果示例:

enter image description here

有人可以告诉我我应该怎么做吗?或者给我举个例子?

编辑:我卡在这里:有人可以帮我完成它吗?我所做的是在带有阴影的白色下制作另一个黑色形状,然后将其全部填充为黑色,这样它就像一个轮廓 - 但我无法弄清楚如何制作阴影的最后和重要部分全部填成黑色。

- (IBAction)addStroke:(id)sender{

[iconStrokeTest setImage:[self makeIconStroke:icon.imageView.image]];

}

- (UIImage *)makeIconStroke:(UIImage *)image{
CGImageRef originalImage = [image CGImage];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
CGImageGetWidth(originalImage),
CGImageGetHeight(originalImage),
8,
CGImageGetWidth(originalImage)*4,
colorSpace,
kCGImageAlphaPremultipliedLast);

CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGBitmapContextGetWidth(bitmapContext), CGBitmapContextGetHeight(bitmapContext)), originalImage);

CGImageRef finalMaskImage = [self createMaskWithImageAlpha:bitmapContext];

UIImage *result = [UIImage imageWithCGImage:finalMaskImage];

CGContextRelease(bitmapContext);
CGImageRelease(finalMaskImage);

// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContext(result.size);

// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();

// set the fill color
[[UIColor blackColor] setFill];

// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, result.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeColorBurn);
CGRect rect = CGRectMake(0, 0, result.size.width, result.size.height);
CGContextDrawImage(context, rect, result.CGImage);

// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextClipToMask(context, rect, result.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);

// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//return the color-burned image
return coloredImg;

}

- (CGImageRef)createMaskWithImageAlpha:(CGContextRef)originalImageContext {

UInt8 *data = (UInt8 *)CGBitmapContextGetData(originalImageContext);

float width = CGBitmapContextGetBytesPerRow(originalImageContext) / 4;
float height = CGBitmapContextGetHeight(originalImageContext);

int strideLength = ROUND_UP(width * 1, 4);
unsigned char * alphaData = (unsigned char * )calloc(strideLength * height, 1);
CGContextRef alphaOnlyContext = CGBitmapContextCreate(alphaData,
width,
height,
8,
strideLength,
NULL,
kCGImageAlphaOnly);

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
unsigned char val = data[y*(int)width*4 + x*4 + 3];
val = 255 - val;
alphaData[y*strideLength + x] = val;
}
}

CGImageRef alphaMaskImage = CGBitmapContextCreateImage(alphaOnlyContext);
CGContextRelease(alphaOnlyContext);
free(alphaData);

// Make a mask
CGImageRef finalMaskImage = CGImageMaskCreate(CGImageGetWidth(alphaMaskImage),
CGImageGetHeight(alphaMaskImage),
CGImageGetBitsPerComponent(alphaMaskImage),
CGImageGetBitsPerPixel(alphaMaskImage),
CGImageGetBytesPerRow(alphaMaskImage),
CGImageGetDataProvider(alphaMaskImage), NULL, false);
CGImageRelease(alphaMaskImage);

return finalMaskImage;
}

最佳答案

嗯,没有内置的 API。你必须自己做或为此找到一个图书馆。但是您可以通过用阴影绘制图像来“伪造”效果。请注意,阴影可以是任何颜色,不必看起来像阴影。这将是最简单的方法。

除此之外,您还可以将光栅图像矢量化并绘制该路径。 Core Image 的边缘检测过滤器将对此有所帮助,但结果可能很难完成。

关于iphone - 如何在图像 (*.png) 周围绘制轮廓/描边 - 内部示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12988328/

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