From 58b6b1068d37370228a63a7cde249d93d1931d82 Mon Sep 17 00:00:00 2001 From: talksik Date: Tue, 14 Dec 2021 16:01:00 -0800 Subject: [PATCH] getting things in place to get average color of an image --- nirvana-ios.xcodeproj/project.pbxproj | 4 +++ nirvana-ios/Globals/RemoteImage.swift | 4 +++ nirvana-ios/Globals/UIImage+Extension.swift | 25 +++++++++++++++ nirvana-ios/Views/Templates/HeaderView.swift | 33 ++++++++------------ 4 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 nirvana-ios/Globals/UIImage+Extension.swift diff --git a/nirvana-ios.xcodeproj/project.pbxproj b/nirvana-ios.xcodeproj/project.pbxproj index 96d23fc..bada989 100644 --- a/nirvana-ios.xcodeproj/project.pbxproj +++ b/nirvana-ios.xcodeproj/project.pbxproj @@ -32,6 +32,7 @@ 89288ECB27675B9F00E962C4 /* OnboardingTemplateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89288ECA27675B9F00E962C4 /* OnboardingTemplateView.swift */; }; 89B46E71276893E200B74255 /* AuthSessionStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89B46E70276893E200B74255 /* AuthSessionStore.swift */; }; 89BDCDE527695DB900577829 /* RemoteImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDE427695DB900577829 /* RemoteImage.swift */; }; + 89BDCDE72769616600577829 /* UIImage+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89BDCDE62769616600577829 /* UIImage+Extension.swift */; }; 89D99D2E2766FD6B00237814 /* Liquid in Frameworks */ = {isa = PBXBuildFile; productRef = 89D99D2D2766FD6B00237814 /* Liquid */; }; 89F1386D2767EB19006680ED /* SignInView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F1386C2767EB19006680ED /* SignInView.swift */; }; 89F138702767F55F006680ED /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 89F1386F2767F55F006680ED /* GoogleSignIn */; }; @@ -61,6 +62,7 @@ 89288ECA27675B9F00E962C4 /* OnboardingTemplateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingTemplateView.swift; sourceTree = ""; }; 89B46E70276893E200B74255 /* AuthSessionStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSessionStore.swift; sourceTree = ""; }; 89BDCDE427695DB900577829 /* RemoteImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteImage.swift; sourceTree = ""; }; + 89BDCDE62769616600577829 /* UIImage+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Extension.swift"; sourceTree = ""; }; 89F1386C2767EB19006680ED /* SignInView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInView.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -177,6 +179,7 @@ 891A15F4276557AB00B26659 /* colors.swift */, 891A15FC276566B800B26659 /* constants.swift */, 89BDCDE427695DB900577829 /* RemoteImage.swift */, + 89BDCDE62769616600577829 /* UIImage+Extension.swift */, ); path = Globals; sourceTree = ""; @@ -324,6 +327,7 @@ 891A15FF27656F9200B26659 /* HeaderView.swift in Sources */, 892179E42765FEB8001E6C60 /* FooterControlsView.swift in Sources */, 891A15FD276566B800B26659 /* constants.swift in Sources */, + 89BDCDE72769616600577829 /* UIImage+Extension.swift in Sources */, 89F1386D2767EB19006680ED /* SignInView.swift in Sources */, 891A160E27657CEA00B26659 /* User.swift in Sources */, ); diff --git a/nirvana-ios/Globals/RemoteImage.swift b/nirvana-ios/Globals/RemoteImage.swift index f2b9dc9..6c5faaf 100644 --- a/nirvana-ios/Globals/RemoteImage.swift +++ b/nirvana-ios/Globals/RemoteImage.swift @@ -59,12 +59,16 @@ struct RemoteImage: View { return failure default: if let image = UIImage(data: loader.data) { + let averageColor = image.averageColor + print("the average color is \(averageColor?.description)") + return Image(uiImage: image) } else { return failure } } } + } struct RemoteImage_Previews: PreviewProvider { diff --git a/nirvana-ios/Globals/UIImage+Extension.swift b/nirvana-ios/Globals/UIImage+Extension.swift new file mode 100644 index 0000000..e7c546a --- /dev/null +++ b/nirvana-ios/Globals/UIImage+Extension.swift @@ -0,0 +1,25 @@ +// +// UIImage+Extension.swift +// nirvana-ios +// +// Created by Arjun Patel on 12/14/21. +// + +import Foundation +import SwiftUI + +extension UIImage { + var averageColor: UIColor? { + guard let inputImage = CIImage(image: self) else { return nil } + let extentVector = CIVector(x: inputImage.extent.origin.x, y: inputImage.extent.origin.y, z: inputImage.extent.size.width, w: inputImage.extent.size.height) + + guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: inputImage, kCIInputExtentKey: extentVector]) else { return nil } + guard let outputImage = filter.outputImage else { return nil } + + var bitmap = [UInt8](repeating: 0, count: 4) + let context = CIContext(options: [.workingColorSpace: kCFNull]) + context.render(outputImage, toBitmap: &bitmap, rowBytes: 4, bounds: CGRect(x: 0, y: 0, width: 1, height: 1), format: .RGBA8, colorSpace: nil) + + return UIColor(red: CGFloat(bitmap[0]) / 255, green: CGFloat(bitmap[1]) / 255, blue: CGFloat(bitmap[2]) / 255, alpha: CGFloat(bitmap[3]) / 255) + } +} diff --git a/nirvana-ios/Views/Templates/HeaderView.swift b/nirvana-ios/Views/Templates/HeaderView.swift index 6c2a6df..812e4f5 100644 --- a/nirvana-ios/Views/Templates/HeaderView.swift +++ b/nirvana-ios/Views/Templates/HeaderView.swift @@ -9,42 +9,35 @@ import SwiftUI struct HeaderView: View { @EnvironmentObject var authStore:AuthSessionStore + private var averageColor: UIColor? var body: some View { HStack(alignment:.center) { - if self.authStore.sessionState == SessionState.isAuthenticated { - Image(systemName: "list.bullet.circle") - .font(Font.system(.largeTitle)) - .padding() - .foregroundColor(NirvanaColor.teal) - } - - Spacer() - - HStack { - Image("undraw_handcrafts_leaf") - .resizable() - .frame(width: 20.0, height: 32.132) - - Text("nirvana") - .font(Font.custom("Satisfy-Regular", size: 35)) - .foregroundColor(NirvanaColor.teal) - .multilineTextAlignment(.center) - } + Image("undraw_handcrafts_leaf") + .resizable() + .frame(width: 20.0, height: 32.132) + .padding(.leading, 20) Spacer() if self.authStore.sessionState == SessionState.isAuthenticated { RemoteImage(url: "https://avatars.githubusercontent.com/u/41487836") .background(NirvanaColor.teal) - .aspectRatio(contentMode: .fit) + .aspectRatio(contentMode: .fill) .frame(width: 40, height: 40) .clipShape(Circle()) .padding() + .shadow(radius:5) + .onAppear { + self.setAverageColor() + } } } .frame(maxWidth: .infinity) } + + private func setAverageColor() { + } } struct HeaderView_Previews: PreviewProvider {