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