choose an image from the gallerypull/22/head
parent
c56dc05f3a
commit
6fa62e8f54
@ -0,0 +1,74 @@
|
||||
//
|
||||
// EditImageComponent.swift
|
||||
// ArkitDoushiQi
|
||||
//
|
||||
// Created by Johan LACHENAL on 31/05/2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import PhotosUI
|
||||
|
||||
struct EditImageComponent: View {
|
||||
@State private var avatarItem: PhotosPickerItem?
|
||||
@State private var avatarImage: Image?
|
||||
let color: Color
|
||||
let profileWidth: CGFloat
|
||||
let profileHeight: CGFloat
|
||||
let defaultImage: Image
|
||||
let imageTextChange: String
|
||||
var body: some View {
|
||||
HStack {
|
||||
|
||||
VStack {
|
||||
HStack {
|
||||
GeometryReader { geometry in
|
||||
ZStack {
|
||||
// Background color
|
||||
color
|
||||
|
||||
// Profile Image
|
||||
ProfileComponent(color: color, profileWidth: profileWidth, profileHeight: profileHeight, image: avatarImage ?? defaultImage
|
||||
)
|
||||
}
|
||||
// Ensure the ZStack takes the size of the GeometryReader
|
||||
.frame(width: geometry.size.width, height: geometry.size.height)
|
||||
.clipShape(Circle())
|
||||
}
|
||||
.aspectRatio(1, contentMode: .fit)
|
||||
.frame(width: profileWidth, height: profileHeight) // Optional fixed size
|
||||
|
||||
PhotosPicker(selection: $avatarItem, matching: .images) {
|
||||
Text(imageTextChange)
|
||||
}
|
||||
.onChange(of: avatarItem) { newValue in
|
||||
if let newItem = newValue {
|
||||
Task {
|
||||
if let data = try? await newItem.loadTransferable(type: Data.self),
|
||||
let uiImage = UIImage(data: data) {
|
||||
avatarImage = Image(uiImage: uiImage)
|
||||
} else {
|
||||
print("Failed to load image")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct EditImageComponent_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
EditImageComponent(
|
||||
color: Color(.red),
|
||||
profileWidth: 100,
|
||||
profileHeight: 100,
|
||||
defaultImage: Image("profil"),
|
||||
imageTextChange: "Changer d'avatar"
|
||||
)
|
||||
.previewLayout(.sizeThatFits)
|
||||
.padding()
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
//
|
||||
// KeyboardReadable.swift
|
||||
// ArkitDoushiQi
|
||||
//
|
||||
// Created by Johan LACHENAL on 31/05/2024.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
import UIKit
|
||||
|
||||
|
||||
// Publisher to read keyboard changes.
|
||||
protocol KeyboardReadable {
|
||||
var keyboardPublisher: AnyPublisher<Bool, Never> { get }
|
||||
}
|
||||
|
||||
extension KeyboardReadable {
|
||||
var keyboardPublisher: AnyPublisher<Bool, Never> {
|
||||
Publishers.Merge(
|
||||
NotificationCenter.default
|
||||
.publisher(for: UIResponder.keyboardWillShowNotification)
|
||||
.map { _ in true },
|
||||
|
||||
NotificationCenter.default
|
||||
.publisher(for: UIResponder.keyboardWillHideNotification)
|
||||
.map { _ in false }
|
||||
)
|
||||
.eraseToAnyPublisher()
|
||||
}
|
||||
}
|
Loading…
Reference in new issue