【问题标题】:How to add text to CIImage?如何向 CIImage 添加文本?
【发布时间】:2014-10-26 04:01:39
【问题描述】:

我正在使用 Photos 框架并创建了一个应用程序,它将对图像应用过滤器。现在,我不想应用过滤器,而是想在图像顶部添加文本。这个 API 为我提供了一个 CIImage,我可以使用它来创建一个输出 CIImage。我只是不知道如何将特定位置的文本添加到CIImage。如果我是正确的,由于性能下降,不建议将其转换为CGImage 然后添加文本。

如何使用现有的CIImage 输出完全相同的CIImage(保留原始图像质量),并在特定位置放置文本?

//Get full image
let url = contentEditingInput.fullSizeImageURL
let orientation = contentEditingInput.fullSizeImageOrientation
var inputImage = CIImage(contentsOfURL: url)
inputImage = inputImage.imageByApplyingOrientation(orientation)

//TODO: REPLACE WITH TEXT OVERLAY
/*//Add filter
let filterName = "CISepiaTone"
let filter = CIFilter(name: filterName)
filter.setDefaults()
filter.setValue(inputImage, forKey: kCIInputImageKey)
let outputImage: CIImage = filter.outputImage*/

//Create editing output
let jpegData: NSData = self.jpegRepresentationOfImage(outputImage)
let adjustmentData = PHAdjustmentData(formatIdentifier: AdjustmentFormatIdentifier, formatVersion: "1.0", data: filterName.dataUsingEncoding(NSUTF8StringEncoding))

let contentEditingOutput = PHContentEditingOutput(contentEditingInput: contentEditingInput)
jpegData.writeToURL(contentEditingOutput.renderedContentURL, atomically: true)
contentEditingOutput.adjustmentData = adjustmentData

PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
    let request = PHAssetChangeRequest(forAsset: asset)
request.contentEditingOutput = contentEditingOutput
}, completionHandler: { (success: Bool, error: NSError!) -> Void in
    if !success {
        NSLog("Error saving image: %@", error)
    }
})

【问题讨论】:

    标签: ios swift core-image cgimage ciimage


    【解决方案1】:

    您可以将灰度文本绘制成单独的CGImage,将CGImage 转换为CIImage(通过[+CIImage imageWithCGImage:]),然后将其用作掩码,将其和原始CIImage 发送到CIBlendWithMask 过滤器。

    【讨论】:

    • 美丽。正是我想要的!
    • swift中有什么例子吗?
    【解决方案2】:

    截至去年,有一个新的CIFilter 可用,称为CIAttributedTextImageGenerator。以下是我如何使用它的示例,包含在实用程序类方法中:

    + (CIImage *)imageWithText:(NSString *)message color:(CIColor *)color scaleFactor:(CGFloat)scaleFactor
    {
        NSDictionary *attributes = @{
            NSForegroundColorAttributeName : CFBridgingRelease(CGColorCreateSRGB(color.red, color.green, color.blue, color.alpha)),
        };
        NSAttributedString *text = [[NSAttributedString alloc] initWithString:message attributes:attributes];
    
        CIFilter<CIAttributedTextImageGenerator> *filter = [CIFilter attributedTextImageGeneratorFilter];
        filter.text = text;
        filter.scaleFactor = scaleFactor;
    
        CIImage *result = filter.outputImage;
        return result;
    }
    

    不幸的是,似乎存在一个错误,不允许您为后续调用此过滤器选择新颜色。 IOW,一旦你渲染这个过滤器一次,每个后续渲染都会产生与第一次渲染相同颜色的文本,不管你传入什么颜色。

    无论如何,这将产生一个CIImage,然后您可以将其覆盖到您的inputImage 上,如下所示:

    CIImage *textImage = [YourUtilityClass imageWithText:@"Some text" color:[CIColor whiteColor] scaleFactor:1.0];
    CIImage *outputImage = [textImage imageByCompositingOverImage:inputImage];
    

    我最近没有太多使用 Swift 的经验,但希望这段 Objective-C 代码足够简单,让你明白这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2016-11-03
      • 1970-01-01
      相关资源
      最近更新 更多