iOS-在创建核心图像滤镜循环时保留原始图像

时间:2018-09-10 20:33:32

标签: ios swift uibutton uiimageview core-image

我关注了这两个教程tutsplusappcoda

我在vc1中有一个图像,该图像传递给vc2。 vc2有两个imageViews:originalImageView显示来自vc1的原始图像,filteredImageView显示应用了核心图像滤镜的新图像。 filteredImageView锚定在originalImageView

的顶部

我还有一个scrollView,我使用按钮来创建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
}

rendition

1 个答案:

答案 0 :(得分:1)

我不是使用scrollView,而是使用了水平collectionView,并在viewDidLoad内第一次将vc1中的图像添加到[UIImages]的数组中,然后第二次遍历所有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]
}