[英]iOS: When applying a CIFilter to a UIImage the result image is rotated
[英]Applying CIFilter to UIImage results in resized and repositioned image
將 CIFilter 應用於相機拍攝的照片后,拍攝的圖像會縮小並重新定位。
我在想,如果我能夠獲得原始圖像的大小和方向,它會相應地縮放並將圖像視圖固定到屏幕的角落。 但是,這種方法沒有任何改變,並且不知道我可以正確地將圖像縮放到屏幕的全尺寸的方法。
func applyBloom() -> UIImage {
let ciImage = CIImage(image: image) // image is from UIImageView
let filteredImage = ciImage?.applyingFilter("CIBloom",
withInputParameters: [ kCIInputRadiusKey: 8,
kCIInputIntensityKey: 1.00 ])
let originalScale = image.scale
let originalOrientation = image.imageOrientation
if let image = filteredImage {
let image = UIImage(ciImage: image, scale: originalScale, orientation: originalOrientation)
return image
}
return self.image
}
圖片說明:圖片截取並截屏,圖片縮小后留空。
嘗試這樣的事情。 代替:
func applyBloom() -> UIImage {
let ciInputImage = CIImage(image: image) // image is from UIImageView
let ciOutputImage = ciInputImage?.applyingFilter("CIBloom",
withInputParameters: [kCIInputRadiusKey: 8, kCIInputIntensityKey: 1.00 ])
let context = CIContext()
let cgOutputImage = context.createCGImage(ciOutputImage, from: ciInputImage.extent)
return UIImage(cgImage: cgOutputImage!)
}
發生的事情是這樣的 - 獲取過濾/輸出 CIImage,並使用 CIContext,編寫輸入 CIImage 大小的 CGImage。
最后,一個建議。 如果您嘗試使用 CIFilter 來顯示對圖像的“近實時”更改(如照片編輯器),請考慮使用 CIImages 和GLKView
對 UIImages 和UIImageView
的主要性能改進。 前者使用設備 GPU 而不是 CPU。
如果CIFilter
輸出尺寸與輸入圖像不同的圖像(例如使用CIPixellate
),也會發生這種情況
在這種情況下,只需告訴CIContext
以較小的矩形呈現圖像:
let cgOutputImage = context.createCGImage(ciOutputImage, from: ciInputImage.extent.insetBy(dx: 20, dy: 20))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.