ios - 如何在 Swift 中以编程方式从另一个图像中剪切 Logo (png 图像)的形状?

标签 ios swift uiimage core-graphics

这是背景图

enter image description here

这是标志

enter image description here

我们如何在 Swift 中制作这样的图像?

enter image description here


更新

现在我设法将 Logo 用作 mask 并得到类似这样的东西, enter image description here

有什么办法可以把mask反转过来吗?

这是我的代码

let logo = UIImage(named: "logo")!
let mask = CALayer()
mask.contents = logo.CGImage
mask.frame = mImageView.layer.bounds
mImageView.layer.mask = mask

最佳答案

您可以使用 UIBezierPath 以编程方式完成此操作:

// lets create a view and an image for testing
let picture = UIImage(data: try! Data(contentsOf: URL(string: "http://i.stack.imgur.com/Xs4RX.jpg")!))!

// lets create a view and an image for testing
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: picture.size.width, height: picture.size.height))
imageView.image = picture

// now a layer for the mask
let maskLayer = CAShapeLayer()

// a path for the logo
let maskPath = CGMutablePath()

// create your logo path (I've added this circle to represent your logo path)
maskPath.addEllipse(in: CGRect(x: imageView.frame.midX - 150, y: imageView.frame.midY - 150, width: 300, height: 300))

// you will need a rectangle that covers the whole image area to intersect with your logo path
maskPath.addRect(CGRect(x: 0, y: 0, width: picture.size.width, height: picture.size.height))

// add the mask to your maskLayer
maskLayer.path = maskPath

// choose the fill rule EvenOdd
maskLayer.fillRule = kCAFillRuleEvenOdd

// add your masklayer to your view
imageView.layer.mask = maskLayer
imageView

如果您需要使用图像并以编程方式反转 Logo 的 alpha,您可以使用 kCGBlendModeDestinationOut 执行以下操作:

import UIKit

extension UIImage {
    func masked(with image: UIImage, position: CGPoint? = nil, inverted: Bool = false) -> UIImage? {
        let position = position ??
            CGPoint(x: size.width.half - image.size.width.half,
                    y: size.height.half - image.size.height.half)
        defer { UIGraphicsEndImageContext() }
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        draw(at: .zero)
        image.draw(at: position, blendMode: inverted ? .destinationOut : .destinationIn, alpha: 1)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

let picture = UIImage(data: try! Data(contentsOf: URL(string: "http://i.stack.imgur.com/Xs4RX.jpg")!))!
let logo = UIImage(data: try! Data(contentsOf: URL(string: "https://www.dropbox.com/s/k7vk3xvcvcly1ik/chat_bubble.png?dl=1")!))!

let view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = .blue
let iv = UIImageView(frame: UIScreen.main.bounds)
iv.contentMode = .scaleAspectFill
iv.image = picture.masked(with: logo, inverted: true)
view.addSubview(iv)

关于ios - 如何在 Swift 中以编程方式从另一个图像中剪切 Logo (png 图像)的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30907448/

相关文章:

iphone - ShareKit - 将链接图像发布到 facebook 墙上

ios - 获取 CollectionViewContentSize

swift - 在 Swift 中从数组创建字典

ios - UIImage 中的颜色变换

xcode - 由于内存错误而终止的应用程序

ios - 如何获取UIDatePickerModeCountDownTimer的 "countDownDuration"值?

ios - 手动将React Native依赖项导入现有的iOS应用

ios - 调整导航后退按钮 iOS 13

Swift Cloud Firestore 自定义对象映射

iOS 如何创建图像从一个 View Controller 到另一个 View Controller 的交互式过渡?