Ios 遮罩周围的图像边框

Ios 遮罩周围的图像边框,ios,objective-c,uiimage,Ios,Objective C,Uiimage,我目前正在使用此代码在我的计算机上创建掩码 现在,我想在屏蔽输出周围添加一个2点边框(如在源url中)。你知道我怎样才能做到这一点吗 谢谢 至少有两个选项: 有三个图像,一个图像将显示在内部,一个图像将用于遮罩第一个图像,然后是第三个图像,即边界(后一个图像将有一个透明的中心)。然后,可以用第二个图像遮罩第一个图像,然后仅用遮罩图像顶部的边框覆盖该图像 但以编程方式为不规则形状的图像生成边框并不容易。使用图形编辑器工具(如PhotoShop或其他工具)创建用于边框的图像可能更容易 与使用图像遮罩

我目前正在使用此代码在我的计算机上创建掩码

现在,我想在屏蔽输出周围添加一个2点边框(如在源url中)。你知道我怎样才能做到这一点吗

谢谢

至少有两个选项:

  • 有三个图像,一个图像将显示在内部,一个图像将用于遮罩第一个图像,然后是第三个图像,即边界(后一个图像将有一个透明的中心)。然后,可以用第二个图像遮罩第一个图像,然后仅用遮罩图像顶部的边框覆盖该图像

    但以编程方式为不规则形状的图像生成边框并不容易。使用图形编辑器工具(如PhotoShop或其他工具)创建用于边框的图像可能更容易

  • 与使用图像遮罩不同,您通常可以为遮罩创建一个
    UIBezierPath
    ,并从该路径创建两个
    CAShapeLayer
    对象,一个用于遮罩图像视图,另一个用于添加边框:

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path addArcWithCenter:CGPointMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0) radius:self.view.bounds.size.width * 0.4 startAngle:0 endAngle:M_PI * 2.0  clockwise:YES];
    
    CAShapeLayer *mask = [CAShapeLayer layer];
    mask.path = path.CGPath;
    mask.fillColor = [[UIColor blackColor] CGColor];
    self.imageView.layer.mask = mask;
    
    CAShapeLayer *border = [CAShapeLayer layer];
    border.path = path.CGPath;
    border.strokeColor = [[UIColor blueColor] CGColor];
    border.fillColor = [[UIColor clearColor] CGColor];
    border.lineWidth = 10.0;
    [self.imageView.layer addSublayer:border]
    
    这是一个例子,路径是一个简单的圆,但你可以创建一个心形的路径或任何东西。贝塞尔路径需要一段时间来适应,但它们对于绘制平滑的曲线形状非常强大


  • 总之,如果边界有一些我可以用贝塞尔曲线生成的规则形状,我更喜欢第二种方法。如果边框是我将在图像编辑工具中手动绘制的,那么我将使用第一种方法。

    请参考下面的链接,我也希望实现同样的效果

    请查找以下代码:

    - (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second
    {
        // get size of the second image
        CGImageRef secondImageRef = second.CGImage;
        CGFloat secondWidth = CGImageGetWidth(secondImageRef);
        CGFloat secondHeight = CGImageGetHeight(secondImageRef);
    
        float offsetwt,offsetht,offset;
    
        offset=20;
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            offset=offset/2;
        }
        offsetht=(secondHeight   * (secondWidth+offset)) /secondWidth;
        offsetwt=secondWidth+offset;
    
        // build merged size
        CGSize mergedSize = CGSizeMake(offsetwt,offsetht);
    
        // capture image context ref
        UIGraphicsBeginImageContext(mergedSize);
    
        //Draw images onto the context
        [first drawInRect:CGRectMake(0, 0, offsetwt, offsetht)];
        [second drawInRect:CGRectMake(offset/2, offset/2, secondWidth, secondHeight) blendMode:kCGBlendModeNormal alpha:1.0];
    
        // assign context to new UIImage
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
        // end context
        UIGraphicsEndImageContext();
    
        return  newImage;
    }
    
    其中第一个参数是用于遮罩的图像,第二个参数是遮罩图像。您可以根据需要设置偏移量。
    我希望这对您有所帮助。

    您有没有可以给我看的代码片段?
    - (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second
    {
        // get size of the second image
        CGImageRef secondImageRef = second.CGImage;
        CGFloat secondWidth = CGImageGetWidth(secondImageRef);
        CGFloat secondHeight = CGImageGetHeight(secondImageRef);
    
        float offsetwt,offsetht,offset;
    
        offset=20;
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            offset=offset/2;
        }
        offsetht=(secondHeight   * (secondWidth+offset)) /secondWidth;
        offsetwt=secondWidth+offset;
    
        // build merged size
        CGSize mergedSize = CGSizeMake(offsetwt,offsetht);
    
        // capture image context ref
        UIGraphicsBeginImageContext(mergedSize);
    
        //Draw images onto the context
        [first drawInRect:CGRectMake(0, 0, offsetwt, offsetht)];
        [second drawInRect:CGRectMake(offset/2, offset/2, secondWidth, secondHeight) blendMode:kCGBlendModeNormal alpha:1.0];
    
        // assign context to new UIImage
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
        // end context
        UIGraphicsEndImageContext();
    
        return  newImage;
    }