gpt4 book ai didi

ios - SwiftUI/在圈内添加照片

转载 作者:行者123 更新时间:2023-12-04 15:09:36 25 4
gpt4 key购买 nike

当我按图片所示放置照片时,同一张照片出现在 3 个圆圈中。

我想为每个圈子添加单独的照片,因此我使用我创建的选择器从相机或画廊添加它们。但是,我做的代码可能是错误的,你能给出正确的逻辑吗?抱歉,我没有足够的信息来更正它。

enter image description here

struct SendPicture: View {
@State private var image: Image? = Image("plus")
@State private var shouldPresentImagePicker = false
@State private var shouldPresentActionScheet = false
@State private var shouldPresentCamera = false

var body: some View {
GeometryReader{ geometry in
VStack{
HStack{
Text("Picture")
.font(.system(size: 60))
.frame(width:geometry.size.width, height: geometry.size.height / 10)
.foregroundColor(.init(red: 45/255, green: 0/255, blue: 112/255))
Spacer().frame(height: geometry.size.height / 5)
}

VStack{
Text("Hi").frame(width:geometry.size.width, height: geometry.size.height / 10)

}
VStack{
Image("AstroGirl").resizable()
.aspectRatio(contentMode: .fit)
.frame(width: geometry.size.width,height: geometry.size.height / 3)

}
HStack{
image!
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: geometry.size.width / 5, height: geometry.size.height / 10)
.clipShape(Circle())
.overlay(Circle().stroke(Color.yellow, lineWidth: 4))
.shadow(radius: 10)
.onTapGesture { self.shouldPresentActionScheet = true }
.sheet(isPresented: $shouldPresentImagePicker) {
SUImagePickerView(sourceType: self.shouldPresentCamera ? .camera : .photoLibrary, image: self.$image, isPresented: self.$shouldPresentImagePicker)
}.actionSheet(isPresented: $shouldPresentActionScheet) { () -> ActionSheet in
ActionSheet(title: Text("Choose mode"), message: Text("Please choose your preferred mode to set your profile image"), buttons: [ActionSheet.Button.default(Text("Camera"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = true
}), ActionSheet.Button.default(Text("Photo Library"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = false
}), ActionSheet.Button.cancel()])
}
image!
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: geometry.size.width / 5, height: geometry.size.height / 10)
.clipShape(Circle())
.overlay(Circle().stroke(Color.yellow, lineWidth: 4))
.shadow(radius: 10)
.onTapGesture { self.shouldPresentActionScheet = true }
.sheet(isPresented: $shouldPresentImagePicker) {
SUImagePickerView(sourceType: self.shouldPresentCamera ? .camera : .photoLibrary, image: self.$image, isPresented: self.$shouldPresentImagePicker)
}.actionSheet(isPresented: $shouldPresentActionScheet) { () -> ActionSheet in
ActionSheet(title: Text("Choose mode"), message: Text("Please choose your preferred mode to set your profile image"), buttons: [ActionSheet.Button.default(Text("Camera"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = true
}), ActionSheet.Button.default(Text("Photo Library"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = false
}), ActionSheet.Button.cancel()])
}
image!
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: geometry.size.width / 5, height: geometry.size.height / 10)
.clipShape(Circle())
.overlay(Circle().stroke(Color.yellow, lineWidth: 4))
.shadow(radius: 10)
.onTapGesture { self.shouldPresentActionScheet = true }
.sheet(isPresented: $shouldPresentImagePicker) {
SUImagePickerView(sourceType: self.shouldPresentCamera ? .camera : .photoLibrary, image: self.$image, isPresented: self.$shouldPresentImagePicker)
}.actionSheet(isPresented: $shouldPresentActionScheet) { () -> ActionSheet in
ActionSheet(title: Text("Choose mode"), message: Text("Please choose your preferred mode to set your profile image"), buttons: [ActionSheet.Button.default(Text("Camera"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = true
}), ActionSheet.Button.default(Text("Photo Library"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = false
}), ActionSheet.Button.cancel()])
}
}
Spacer().frame(height: geometry.size.height / 10)
VStack{
Button(action: {

}, label: {
Text("Giriş Yap")
.frame(width: geometry.size.width / 3, height: 50)
.padding(10)
.font(Font.system(size: 30, weight: .medium, design: .serif))
.foregroundColor(.white)
.background(RoundedRectangle(cornerRadius: 30))
.foregroundColor(.init(red: 45/255, green: 0/255, blue: 112/255))

})
}
}
}
}
}
import SwiftUI
import UIKit

struct SUImagePickerView: UIViewControllerRepresentable {

var sourceType: UIImagePickerController.SourceType = .photoLibrary
@Binding var image: Image?
@Binding var isPresented: Bool

func makeCoordinator() -> ImagePickerViewCoordinator {
return ImagePickerViewCoordinator(image: $image, isPresented: $isPresented)
}

func makeUIViewController(context: Context) -> UIImagePickerController {
let pickerController = UIImagePickerController()
pickerController.sourceType = sourceType
pickerController.delegate = context.coordinator
return pickerController
}

func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
// Nothing to update here
}
}

class ImagePickerViewCoordinator: NSObject, UINavigationControllerDelegate,
UIImagePickerControllerDelegate {

@Binding var image: Image?
@Binding var isPresented: Bool

init(image: Binding<Image?>, isPresented: Binding<Bool>) {
self._image = image
self._isPresented = isPresented
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self.image = Image(uiImage: image)
}
self.isPresented = false
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.isPresented = false
}
}

最佳答案

正如 Duncan C 所观察到的,您正在为所有 ImageView 使用相同的 @State 属性。

一种可能的解决方案是将重复代码提取到新 View 中:

struct IconView: View {
@State private var image: Image?
@State private var shouldPresentImagePicker = false
@State private var shouldPresentActionScheet = false
@State private var shouldPresentCamera = false

var imageView: Image {
image ?? Image("plus")
}

var body: some View {
imageView
.resizable()
.aspectRatio(contentMode: .fill)
.clipShape(Circle())
.overlay(Circle().stroke(Color.yellow, lineWidth: 4))
.onTapGesture { self.shouldPresentActionScheet = true }
.sheet(isPresented: $shouldPresentImagePicker) {
SUImagePickerView(sourceType: self.shouldPresentCamera ? .camera : .photoLibrary, image: self.$image, isPresented: self.$shouldPresentImagePicker)
}
.actionSheet(isPresented: $shouldPresentActionScheet) { () -> ActionSheet in
ActionSheet(title: Text("Choose mode"), message: Text("Please choose your preferred mode to set your profile image"), buttons: [ActionSheet.Button.default(Text("Camera"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = true
}), ActionSheet.Button.default(Text("Photo Library"), action: {
self.shouldPresentImagePicker = true
self.shouldPresentCamera = false
}), ActionSheet.Button.cancel()])
}
}
}

然后在您的 HStack 中使用此 View :

struct SendPicture: View {
var body: some View {
GeometryReader { geometry in
VStack {
...
HStack {
ForEach(0 ..< 3) { _ in
IconView()
.frame(width: geometry.size.width / 5, height: geometry.size.height / 10)
.shadow(radius: 10)
}
}
...
}
}
}
}

整个 SendPicture View 看起来像这样:

struct SendPicture: View {
var body: some View {
GeometryReader { geometry in
VStack {
HStack {
Text("Picture")
.font(.system(size: 60))
.frame(width: geometry.size.width, height: geometry.size.height / 10)
.foregroundColor(.init(red: 45 / 255, green: 0 / 255, blue: 112 / 255))
Spacer().frame(height: geometry.size.height / 5)
}

VStack {
Text("Hi").frame(width: geometry.size.width, height: geometry.size.height / 10)
}
VStack {
Image("AstroGirl").resizable()
.aspectRatio(contentMode: .fit)
.frame(width: geometry.size.width, height: geometry.size.height / 3)
}
// This `HStack` is changed
HStack {
ForEach(0 ..< 3) { _ in
IconView()
.frame(width: geometry.size.width / 5, height: geometry.size.height / 10)
.shadow(radius: 10)
}
}
Spacer().frame(height: geometry.size.height / 10)
VStack {
Button(action: {}, label: {
Text("Giriş Yap")
.frame(width: geometry.size.width / 3, height: 50)
.padding(10)
.font(Font.system(size: 30, weight: .medium, design: .serif))
.foregroundColor(.white)
.background(RoundedRectangle(cornerRadius: 30))
.foregroundColor(.init(red: 45 / 255, green: 0 / 255, blue: 112 / 255))

})
}
}
}
}
}

关于ios - SwiftUI/在圈内添加照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65451683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com