Skip to content

Commit

Permalink
Enable two preset filters on FilterableImage
Browse files Browse the repository at this point in the history
  • Loading branch information
theInternauts committed Jun 30, 2020
1 parent 7a358ac commit 7d556af
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 73 deletions.
4 changes: 4 additions & 0 deletions PlasticBullet/PlasticBullet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
6D63018623D6C88A005DA6FF /* todo.md in Resources */ = {isa = PBXBuildFile; fileRef = 6D63018323D6C88A005DA6FF /* todo.md */; };
6D63018723D6C88A005DA6FF /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 6D63018423D6C88A005DA6FF /* README.md */; };
6D63018823D6C88A005DA6FF /* LICENSE in Resources */ = {isa = PBXBuildFile; fileRef = 6D63018523D6C88A005DA6FF /* LICENSE */; };
6D79DCF42472DE9200337D65 /* FilterableImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D79DCF32472DE9200337D65 /* FilterableImage.swift */; };
6DA3373D23CAF5350082B22C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DA3373C23CAF5350082B22C /* AppDelegate.swift */; };
6DA3373F23CAF5350082B22C /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DA3373E23CAF5350082B22C /* SceneDelegate.swift */; };
6DA3374123CAF5350082B22C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DA3374023CAF5350082B22C /* ContentView.swift */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
6D63018323D6C88A005DA6FF /* todo.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = todo.md; path = ../todo.md; sourceTree = "<group>"; };
6D63018423D6C88A005DA6FF /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
6D63018523D6C88A005DA6FF /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
6D79DCF32472DE9200337D65 /* FilterableImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterableImage.swift; sourceTree = "<group>"; };
6DA3373923CAF5350082B22C /* PlasticBullet.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PlasticBullet.app; sourceTree = BUILT_PRODUCTS_DIR; };
6DA3373C23CAF5350082B22C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
6DA3373E23CAF5350082B22C /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -154,6 +156,7 @@
6D00463A23CC6A3D00401B4D /* BTN_Info.swift */,
6D00463C23CC6A4C00401B4D /* BTN_Camera.swift */,
6D00463E23CC6A5B00401B4D /* BTN_Library.swift */,
6D79DCF32472DE9200337D65 /* FilterableImage.swift */,
6D3F3C8523FC22EA000980FF /* FilterView.swift */,
6D00464223CC711500401B4D /* Panel_Info.swift */,
6DA3374423CAF5360082B22C /* Preview Content */,
Expand Down Expand Up @@ -390,6 +393,7 @@
6D00463B23CC6A3D00401B4D /* BTN_Info.swift in Sources */,
6D00463F23CC6A5B00401B4D /* BTN_Library.swift in Sources */,
6D00463D23CC6A4C00401B4D /* BTN_Camera.swift in Sources */,
6D79DCF42472DE9200337D65 /* FilterableImage.swift in Sources */,
6DA3373F23CAF5350082B22C /* SceneDelegate.swift in Sources */,
6D00464323CC711500401B4D /* Panel_Info.swift in Sources */,
6D3F3C8423F30EC8000980FF /* ImagePicker.swift in Sources */,
Expand Down
3 changes: 1 addition & 2 deletions PlasticBullet/PlasticBullet/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ struct ContentView: View {

func loadRandomImage() {
// .background(UIImage(contentsOfFile: "160421-IMG_5876-"))
let selectedBgImageIndex = getRandomIndex(max: (backgroundImageFilenames.endIndex-1))
let selectedBgImageIndex = getRandomIndex(max: (backgroundImageFilenames.count-1))
bgImage = Image(backgroundImageFilenames[selectedBgImageIndex])
}

func getRandomIndex(max: Int) -> Int {
let max = backgroundImageFilenames.endIndex
return Int.random(in: 0...max)
}
}
Expand Down
78 changes: 7 additions & 71 deletions PlasticBullet/PlasticBullet/FilterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,13 @@ struct FilterView: View {
@Binding var isShowingImagePicker: Bool
var source: UIImagePickerController.SourceType = .photoLibrary
@Binding var selectedImage: UIImage?
@State private var baseImages: [UIImage] = [UIImage(imageLiteralResourceName: "160421-IMG_5876-"),UIImage(imageLiteralResourceName: "160421-IMG_5876-"),UIImage(imageLiteralResourceName: "160421-IMG_5876-"),UIImage(imageLiteralResourceName: "160421-IMG_5876-")]

var dupleImage: some View {
return HStack {
Spacer()
Image(uiImage: baseImages[0])
.resizable()
.scaledToFit()
Spacer()
Image(uiImage: baseImages[1])
.resizable()
.scaledToFit()
Spacer()
}
// .onAppear(perform: preloadImages)
}


var body: some View {
VStack {
dupleImage
dupleImage
List {
FilterableImage(selectedImage: $selectedImage, processedImage: selectedImage)
FilterableImage(selectedImage: $selectedImage, processedImage: selectedImage)
}
Spacer()
Button(action: {
self.isShowingImagePicker.toggle()
Expand All @@ -43,60 +29,10 @@ struct FilterView: View {
}
Spacer()
}
.navigationBarTitle(Text("Filters will be applied here"))

.sheet(isPresented: $isShowingImagePicker, onDismiss: loadImage){
.sheet(isPresented: $isShowingImagePicker) {
ImagePicker(image: self.$selectedImage, source: self.source)
}.navigationBarTitle(Text("Filters will be applied here"))
}

mutating func preloadImages(){

// baseImages = Image(uiImage: selectedImage)
baseImages[0] = UIImage(imageLiteralResourceName: "160421-IMG_5876-")
baseImages[1] = UIImage(imageLiteralResourceName: "160421-IMG_5876-")
baseImages[2] = UIImage(imageLiteralResourceName: "160421-IMG_5876-")
baseImages[3] = UIImage(imageLiteralResourceName: "160421-IMG_5876-")
}

func swapImags(newImages: [UIImage]){
self.baseImages = newImages
}

func loadImage() {
print("1- sel - image:", selectedImage! )
guard let selectedImage = selectedImage else { return }
print("2- sel - image:", selectedImage )
var newImages:[UIImage] = []
for _ in baseImages.indices {
let tempImg = processImage(image: selectedImage)
print("3- sel: ", tempImg)
// newImages.append(Image(uiImage: tempImg))
newImages.append(tempImg)

}
// self.baseImages = newImages
self.swapImags(newImages: newImages)

}

func applyBlur(image: CIImage) -> CIImage {
let blur = CIFilter.gaussianBlur()
blur.inputImage = image
blur.radius = 30

guard let output = blur.outputImage else {
return image
}
return output
}

func processImage(image: UIImage) -> UIImage {
print("starting proc image")
guard let coreImage = CIImage(image: image) else { return image }
let output = applyBlur(image: coreImage)
print("returning proc image")
return UIImage(ciImage: output)
.navigationBarTitle(Text("Other filters will be applied here"))
}
}

Expand Down
72 changes: 72 additions & 0 deletions PlasticBullet/PlasticBullet/FilterableImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// FilterableImage.swift
// PlasticBullet
//
// Created by Christopher Wallace on 5/19/20.
// Copyright © 2020 Zinc Collective, LLC. All rights reserved.
//

import SwiftUI

struct Filter {
let filterName: String
var filterEffectValue: Any?
var filterEffectValueName: String?

init(filterName: String, filterEffectValue: Any?, filterEffectValueName: String?) {
self.filterName = filterName
self.filterEffectValue = filterEffectValue
self.filterEffectValueName = filterEffectValueName
}
}

struct FilterableImage: View {
@Binding var selectedImage: UIImage?
@State var processedImage: UIImage?

// CORE IMAGE FILTERS REFERENCE: "https://developer.apple.com/library/archive/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/uid/TP40004346-Reference"
@State var sepia: Filter = Filter(filterName: "CISepiaTone", filterEffectValue: 0.95, filterEffectValueName: kCIInputIntensityKey)
@State var gaussianBlur: Filter = Filter(filterName: "CIGaussianBlur", filterEffectValue: 20, filterEffectValueName: kCIInputRadiusKey)

var body: some View {
Image(uiImage: self.processedImage!)
.resizable()
.scaledToFit()
.border(Color.red, width: 4)
.onTapGesture(perform: {self.updateFliterableImageView()})
.onAppear(perform: setStartImageState)
}

func updateFliterableImageView() -> Void {
print("tapped on a FliterableImageView")
self.gaussianBlur.filterEffectValue = 50
self.processedImage = self.processImage(image: self.processedImage!, filterEffect: self.gaussianBlur)
}

func setStartImageState() -> Void {
print("setStartImageState()")
self.processedImage = self.processImage(image: self.selectedImage!, filterEffect: self.gaussianBlur)
}

func processImage(image: UIImage, filterEffect: Filter) -> UIImage {
print("starting proc image")
return applyFilterTo(image: image, filterEffect: filterEffect)
}

func applyFilterTo(image: UIImage, filterEffect: Filter) -> UIImage {
let context = CIContext()
let filter = CIFilter(name: filterEffect.filterName)
filter?.setValue(filterEffect.filterEffectValue!, forKey: filterEffect.filterEffectValueName!)
filter?.setValue(CIImage(image: image), forKey: kCIInputImageKey)
guard let output = filter?.outputImage else {
return image
}
return UIImage(cgImage: context.createCGImage(output, from: output.extent) ?? image as! CGImage)
}
}
//
//struct FilterableImage_Previews: PreviewProvider {
// static var previews: some View {
// FilterableImage(selectedImage: .constant(UIImage(imageLiteralResourceName: "160421-IMG_5876-")))
// }
//}
1 change: 1 addition & 0 deletions PlasticBullet/PlasticBullet/ImagePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct ImagePicker: UIViewControllerRepresentable {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let uiImage = info[.originalImage] as? UIImage {
parent.image = uiImage
print("picked: ", parent.image as Any)
}

parent.presentationMode.wrappedValue.dismiss()
Expand Down

0 comments on commit 7d556af

Please sign in to comment.