iOS - 在创建核心图像过滤器循环时保留原始图像
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS - 在创建核心图像过滤器循环时保留原始图像相关的知识,希望对你有一定的参考价值。
我在vc1中有一个图像,我传递给vc2。 vc2有两个imageViews:originalImageView
,显示来自vc1和filteredImageView
的原始图像,显示应用了核心图像过滤器的新图像。 filteredImageView
停泊在originalImageView
之上
我还有一个scrollView,我使用as按钮创建coreImage过滤器,一旦你按下一个按钮就会应用新的过滤器,过滤的图像现在显示在filteredImageView
里面,它隐藏了originalImageView
,因为它位于它下面。
一旦用户按下包含过滤器的任何按钮,他们就无法返回到原始图像。
如何创建一个按钮,在scrollView中显示原始图像以及所有已过滤的图像?理想情况下,它将是第一个图像,以便一旦用户按下它,就会显示原始图像
let originalImageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
return imageView
}()
let filteredImageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
return imageView
}()
let scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = .black
return scrollView
}()
var originalImage: UIImage? // set from vc1
let ciFilterNames = [
"CISharpenLuminance",
"CIPhotoEffectChrome",
"CIPhotoEffectFade",
"CIPhotoEffectInstant",
"CIPhotoEffectNoir",
"CIPhotoEffectProcess",
"CIPhotoEffectTonal",
"CIPhotoEffectTransfer",
"CISepiaTone"
]
override func viewDidLoad() {
super.viewDidLoad()
createAnchors()
guard let originalImage = originalImage else { return }
originaImage.image = originalImage
var xCoord: CGFloat = 5
let yCoord: CGFloat = 5
let buttonWidth:CGFloat = 70
let buttonHeight: CGFloat = 70
let gapBetweenButtons: CGFloat = 5
var itemCount = 0
for i in 0..<ciFilterNames.count {
itemCount = i
let filterButton = UIButton(type: .custom)
button.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
filterButton.tag = itemCount
filterButton.addTarget(self, action: #selector(filterButtonPressed(_:)), for: .touchUpInside)
filterButton.layer.cornerRadius = 7
filterButton.clipsToBounds = true
guard let openGLContext = EAGLContext(api: .openGLES3) else { return }
let context = CIContext(eaglContext: openGLContext)
// Create filters for each button
let coreImage = CIImage(image: originalImage)
let filter = CIFilter(name: "(ciFilterNames[i])" )
filter?.setDefaults()
filter?.setValue(coreImage, forKey: kCIInputImageKey)
if ciFilterNames[i] == "CISharpenLuminance" {
filter?.setValue(0.8, forKey: kCIInputSharpnessKey)
} else {
filter?.setValue(1, forKey: kCIInputIntensityKey)
}
guard let output = filter?.value(forKey: kCIOutputImageKey) as? CIImage else { return }
guard let cgimgresult = context.createCGImage(output, from: output.extent) else { return }
let imageForButton = UIImage(cgImage: cgimgresult)
filterButton.setBackgroundImage(imageForButton, for: .normal)
// Add Buttons in the Scroll View
xCoord += buttonWidth + gapBetweenButtons
scrollView.addSubview(filterButton)
}
}
@objc fileprivate func filterButtonPressed(_ sender: UIButton) {
let button = sender as UIButton
filteredImageView.image = button.backgroundImage( for: .normal)
}
func createAnchors() {
view.addSubview(originalImageView)
view.addSubview(filteredImageView)
view.addSubview(scrollView)
originalImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
originalImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
originalImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
originalImageView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
originalImageView.heightAnchor.constraint(equalToConstant: view.frame.width).isActive = true
filteredImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
filteredImageView.leadingAnchor.constraint(equalTo: originalImageView.leadingAnchor).isActive = true
filteredImageView.widthAnchor.constraint(equalTo: originalImageView.widthAnchor).isActive = true
filteredImageView.heightAnchor.constraint(equalTo: originalImageView.heightAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: originalImageView.bottomAnchor, constant: 8).isActive = true
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
scrollView.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
答案
而不是scrollView我使用水平collectionView和viewDidLoad 1st我将图像从vc1添加到[UIImages]
数组然后第二个I循环遍历所有ciFilterNames并将它们添加到数组中,就像我将它们添加到scrollView一样。通过这种方式,我可以将原始图像作为第一张图像,同时将所有过滤后的图像放在collectionVIew中。我也只需要一个imageView。
我在viewDidLoad中的注释中添加了两个步骤
var collectionView: UICollectionView!
let originalImageView: UIImageView = {
let imageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
return imageView
}()
var originalImage: UIImage? // set from vc1 or just use your own UIImage(named: "whatever") for this example
var tableData = [UIImage]()
let filterCell = "filterCell"
let ciFilterNames = [
"CISharpenLuminance",
"CIPhotoEffectChrome",
"CIPhotoEffectFade",
"CIPhotoEffectInstant",
"CIPhotoEffectNoir",
"CIPhotoEffectProcess",
"CIPhotoEffectTonal",
"CIPhotoEffectTransfer",
"CISepiaTone"
]
override func viewDidLoad() {
super.viewDidLoad()
configureCollectionView()
createAnchors()
guard let originalImage = originalImage else { return }
originaImage.image = originalImage
// 1. this appends the originalImage as the very first cell
tableData.append(originaImage)
guard let openGLContext = EAGLContext(api: .openGLES3) else { return }
let context = CIContext(eaglContext: openGLContext)
for i in 0..<ciFilterNames.count {
let coreImage = CIImage(image: tableData[0])
let filter = CIFilter(name: "(ciFilterNames[i])" )
filter?.setDefaults()
filter?.setValue(coreImage, forKey: kCIInputImageKey)
if ciFilterNames[i] == "CISharpenLuminance" {
filter?.setValue(0.8, forKey: kCIInputSharpnessKey)
} else if ciFilterNames[i] == "CISepiaTone" {
filter?.setValue(1, forKey: kCIInputIntensityKey)
} else {
filter?.value(forKey: kCIOutputImageKey) as! CIImage
}
guard let output = filter?.value(forKey: kCIOutputImageKey) as? CIImage else { return }
guard let cgimgresult = context.createCGImage(output, from: output.extent) else { return }
let newImageWithFilterApplied = UIImage(cgImage: cgimgresult)
// 2. after each loop append the newImageWithFilterApplied to the rest of the cells
tableData.append(newImageWithFilterApplied)
collectionView.reloadData()
}
}
func configureCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
layout.scrollDirection = .horizontal
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceHorizontal = true
collectionView.backgroundColor = .black
collectionView.showsHorizontalScrollIndicator = false
collectionView.register(FilterCell.self, forCellWithReuseIdentifier: filterCell)
}
func createAnchors() {
view.addSubview(originalImageView)
view.addSubview(collectionView)
originalImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
originalImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
originalImageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
originalImageView.heightAnchor.constraint(equalToConstant: view.frame.width).isActive = true
collectionView.topAnchor.constraint(equalTo: originalImageView.bottomAnchor, constant: 10).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tableData.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 80, height: 80)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: filterCell, for: indexPath) as! FilterCell
cell.imageView.image = tableData[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath) as? FilterCell else { return }
guard let indexPath = collectionView.indexPath(for: cell) else { return }
originalImageView.image = tableData[indexPath.item]
}
以上是关于iOS - 在创建核心图像过滤器循环时保留原始图像的主要内容,如果未能解决你的问题,请参考以下文章