1
我正在使用Photos框架,並且已經創建了一個將過濾器應用於圖像的應用程序。現在不用應用濾鏡,我想在圖像上添加文本。該API爲我提供了一個CIImage
,我可以使用它來創建輸出CIImage
。我只是不知道如何在特定位置添加文字到CIImage
。如果我是正確的,建議不要將其轉換爲CGImage
,然後由於性能下降而添加文本。如何將文本添加到CIImage?
如何使用現有的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)
}
})
美麗。正是我在找的! – Joey 2014-09-02 05:04:06
你能用小代碼示例顯示這個嗎? – 2016-05-18 14:51:41
swift中的任何示例? – user924 2018-03-01 14:00:45