You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.5 KiB

//
// 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()
}
}