getting things in place to get average color of an image

This commit is contained in:
talksik
2021-12-14 16:01:00 -08:00
parent 0e73234418
commit 58b6b1068d
4 changed files with 46 additions and 20 deletions
+4
View File
@@ -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 = "<group>"; };
89B46E70276893E200B74255 /* AuthSessionStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSessionStore.swift; sourceTree = "<group>"; };
89BDCDE427695DB900577829 /* RemoteImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteImage.swift; sourceTree = "<group>"; };
89BDCDE62769616600577829 /* UIImage+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Extension.swift"; sourceTree = "<group>"; };
89F1386C2767EB19006680ED /* SignInView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -177,6 +179,7 @@
891A15F4276557AB00B26659 /* colors.swift */,
891A15FC276566B800B26659 /* constants.swift */,
89BDCDE427695DB900577829 /* RemoteImage.swift */,
89BDCDE62769616600577829 /* UIImage+Extension.swift */,
);
path = Globals;
sourceTree = "<group>";
@@ -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 */,
);
+4
View File
@@ -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 {
@@ -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)
}
}
+13 -20
View File
@@ -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 {