【问题标题】:How to set a proper location for CIAttributedTextImageGenerator?如何为 CIAttributedTextImageGenerator 设置合适的位置?
【发布时间】:2021-01-12 15:46:04
【问题描述】:

我使用"CIAttributedTextImageGenerator" 从文本生成CIImage,然后用"CISourceAtopCompositing" 将其覆盖在我编辑的图像上:

    // Text to image
    let font = UIFont.systemFont(ofSize: 78)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    paragraphStyle.firstLineHeadIndent = 5.0
    
    let shadow = NSShadow()
    shadow.shadowColor = UIColor.red
    shadow.shadowBlurRadius = 5

    let attributes: [NSAttributedString.Key: Any] = [
        .font: font,
        .foregroundColor: UIColor.blue,
        .paragraphStyle: paragraphStyle,
        .shadow: shadow
    ]

    let attributedQuote = NSAttributedString(string: "General Kenobi", attributes: attributes)
    let textGenerationFilter = CIFilter(name: "CIAttributedTextImageGenerator")!
    textGenerationFilter.setValue(attributedQuote, forKey: "inputText")
    textGenerationFilter.setValue(NSNumber(value: Double(inputSizeFactor)), forKey: "inputScaleFactor")
    let textImage = textGenerationFilter.outputImage!.oriented(.right)
    
    
    finalImage = textImage
        .applyingFilter("CISourceAtopCompositing", parameters: [ kCIInputBackgroundImageKey: finalImage])

但是,文本图像总是在我编辑的图像的右下角:

如何将其设置为自定义位置?例如,如果我希望它在右下角,或者在编辑图像的中心?

【问题讨论】:

    标签: swift core-image cifilter ciimage


    【解决方案1】:

    您可以在将图像合成到背景之前对其应用变换:

    let transformedText = textImage.transformed(by: CGAffineTransform(translationX: 200, y: 300)
    

    恐怕您必须自己计算使图像居中的确切位置。没有内置方法可以将一张图片居中放置在另一张图片上。

    【讨论】:

      猜你喜欢
      • 2011-10-10
      • 1970-01-01
      • 2021-10-03
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2011-08-25
      • 1970-01-01
      相关资源
      最近更新 更多