使用 UITableView 中的核心图形将图像过滤器应用于图像
Applied image filter to images using core graphics in UITableView
我已经使用核心图形在 table 视图中以灰度显示图像。但是 table 滚动得非常 slowly.Is 有任何解决方案可以加快滚动速度。
您可能需要按照以下步骤操作:
- 假设您在
viewDidLoad
中准备了一组图像,称为 originalImages
。
- 在您的
viewDidLoad
中,您必须滚动所有 originalImages
应用到每个图像的灰度过滤器并将新图像附加到名为 grayScaleImages
的新数组中。
- 使用这样的
grayScaleImages
作为您的 UITableViewDataSource
(和代表)的支点。
一个非常粗略的例子可能是:
import UIKit
class ViewController:UIViewController, UITableViewDataSource {
var originalImages:[UIImage]?
var grayScaleImages:[UIImage]?
override func viewDidLoad() {
super.viewDidLoad()
// load original images
self.originalImages = [UIImage]()
//... load whatever here (eg: from bundle) ...
// load gray scale images
self.grayScaleImages = [UIImage]()
self.originalImages?.forEach { image in
let grayScaleImage = image.applyGrayScale()
self.grayScaleImages?.append(grayScaleImage)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.grayScaleImages?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellID", for: indexPath)
cell.imageView?.image = self.grayScaleImages?[indexPath.row]
return cell
}
}
extension UIImage {
func applyGrayScale() -> UIImage {
let filter = CIFilter(name: "CIPhotoEffectNoir")!
filter.setValue(CIImage(image: self), forKey: kCIInputImageKey)
let output = filter.outputImage!
let cgImage = CIContext(options: nil).createCGImage(output, from: output.extent)!
return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
}
}
我已经使用核心图形在 table 视图中以灰度显示图像。但是 table 滚动得非常 slowly.Is 有任何解决方案可以加快滚动速度。
您可能需要按照以下步骤操作:
- 假设您在
viewDidLoad
中准备了一组图像,称为originalImages
。 - 在您的
viewDidLoad
中,您必须滚动所有originalImages
应用到每个图像的灰度过滤器并将新图像附加到名为grayScaleImages
的新数组中。 - 使用这样的
grayScaleImages
作为您的UITableViewDataSource
(和代表)的支点。
一个非常粗略的例子可能是:
import UIKit
class ViewController:UIViewController, UITableViewDataSource {
var originalImages:[UIImage]?
var grayScaleImages:[UIImage]?
override func viewDidLoad() {
super.viewDidLoad()
// load original images
self.originalImages = [UIImage]()
//... load whatever here (eg: from bundle) ...
// load gray scale images
self.grayScaleImages = [UIImage]()
self.originalImages?.forEach { image in
let grayScaleImage = image.applyGrayScale()
self.grayScaleImages?.append(grayScaleImage)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.grayScaleImages?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellID", for: indexPath)
cell.imageView?.image = self.grayScaleImages?[indexPath.row]
return cell
}
}
extension UIImage {
func applyGrayScale() -> UIImage {
let filter = CIFilter(name: "CIPhotoEffectNoir")!
filter.setValue(CIImage(image: self), forKey: kCIInputImageKey)
let output = filter.outputImage!
let cgImage = CIContext(options: nil).createCGImage(output, from: output.extent)!
return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation)
}
}