admin管理员组文章数量:1396092
I have been working on getting the camera view to work within SwiftUI. I have been able to get it to show and or select a photo. However i have not been able to get it to save to swift data. i'm sure it is something stupid that i am missing but any help would be appreciated. The biggest thing is trying to get the photo and then converting it from UIimage so it can be stored in swift data. review.foodImage is where I need to get it.
Edit Meal View
import SwiftUI
import PhotosUI
import SwiftData
import UIKit
struct EditMealView: View {
@Binding var isPresentingNewMealView: Bool
@Environment(\.modelContext) private var modelContext
@State var mealImage: Image? = nil
@State private var selectedItem: PhotosPickerItem? = nil
@Bindable var review: Review
@State private var imageData: Data?
@State var selection: TextSelection? = nil
@State private var locationService = LocationService(completer: .init())
@State private var search: String = ""
@State private var isPresentingLocationView = false
@State private var sourceType: UIImagePickerController.SourceType = .photoLibrary
@State private var selectedImage: UIImage?
@State private var isImagePickerDisplay = false
let foodTypeList: [String] = ["Fast Food", "Pub Food", "Vegetarian", "Central African", "Happy Hour", "Dog Friendly", "Family Friendly", "East African", "North African", "Southern Africa", "West African", "Latin American", "Native American", "Canadian", "Carribean", "Mexican", "American", "Southern American", "Fusion", "Central American", "South American", "Chinese", "Japanese", "Korean", "Indian", "Middle Eastern", "Thai", "Turkish", "Vietnamese", "Italian", "French", "German", "Spanish", "Greek", "Romanian", "Russian", "Eastern European", "Scandinavian", "British", "Dutch", "Swedish", "Norwegian", "Icelandic", "Irish", "Polynesian", "Australian", "New Zealand", "Other"].sorted()
var body: some View {
NavigationStack {
ZStack {
LinearGradient(gradient: Gradient(colors: [Color(red: 0.9725490196078431, green: 0.9607843137254902, blue: 0.8627450980392157, opacity: 1), Color(red: 0.9137254901960784, green: 0.9254901960784314, blue: 0.8509803921568627, opacity: 1), Color(red: 0.9882352941176471, green: 0.7529411764705882, blue: 0.13333333333333333, opacity: 1), Color(red: 0.9450980392156862, green: 0.47058823529411764, blue: 0.08627450980392157, opacity: 1)]), startPoint: .top, endPoint: .bottom).ignoresSafeArea()
.navigationTitle("Add Meal")
VStack{
Form {
TextField("Menu Item", text: $review.foodName)
.listRowBackground(Color.clear)
.autocorrectionDisabled()
HStack {
Button(action: {isPresentingLocationView = true}
) {
Text("Location")
Image(systemName: "map")
}
.sheet(isPresented: $isPresentingLocationView) {
AddressView(isPresentingLocationView: $isPresentingLocationView, review: review)
}
VStack {
Text("\(review.restaurantName)")
.bold()
.listRowBackground(Color.clear)
.autocorrectionDisabled()
Text("\(review.location)")
.listRowBackground(Color.clear)
.autocorrectionDisabled()
}
.padding()
}
.listRowBackground(Color.clear)
Picker(selection: $review.foodType, label: Text("Food Category")) {
ForEach(0..<foodTypeList.count, id: \.self) {
Text(self.foodTypeList[$0])
.tag(self.foodTypeList[$0])
}
}
.listRowBackground(Color.clear)
HStack{
Spacer()
RatingView(rating: $review.rating)
.multilineTextAlignment(TextAlignment.center)
Spacer()
}
.buttonStyle(.plain)
.listRowBackground(Color.clear)
if let imageData = review.foodImage, let uiImage = UIImage(data: imageData) {
Image(uiImage: uiImage)
.resizable()
.scaledToFit()
}
HStack{
Spacer()
Button("", systemImage: "camera.fill")
{
self.sourceType = .camera
self.isImagePickerDisplay.toggle()
print("camera")
}.padding()
Button("", systemImage: "photo.fill.on.rectangle.fill") {
self.sourceType = .photoLibrary
self.isImagePickerDisplay.toggle()
print( "photo library")
}.padding()
Spacer()
}
.sheet(isPresented: self.$isImagePickerDisplay) {
ImagePickerView(selectedImage: self.$selectedImage, sourceType: self.sourceType)
}
.listRowBackground(Color.clear)
.padding()
.buttonStyle(BorderlessButtonStyle())
Text("Notes")
.font(.headline)
.listRowBackground(Color.clear)
TextEditor(text: $review.reviewNotes, selection: $selection)
.background(Color.white.opacity(0.40))
.listRowBackground(Color.clear)
}
.listRowBackground(Color.clear)
.padding()
Spacer()
}
}
.scrollDismissesKeyboard(.immediately)
.scrollContentBackground(.hidden)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Dismiss") {
isPresentingNewMealView = false
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
let newReview = review
review.lastModified = Date.now
modelContext.insert(newReview)
isPresentingNewMealView = false
}
}
}
}
}
func loadPhoto() {
Task { @MainActor in
review.foodImage = selectedImage?.pngData()
}
}
}
Image picker view
import SwiftUI
import UIKit
struct ImagePickerView: UIViewControllerRepresentable {
@Binding var selectedImage: UIImage?
@Environment(\.presentationMode) var isPresented
var sourceType: UIImagePickerController.SourceType
func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = self.sourceType
return imagePicker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {
}
// Connecting the Coordinator class with this struct
func makeCoordinator() -> Coordinator {
return Coordinator(picker: self)
}
}
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var picker: ImagePickerView
init(picker: ImagePickerView) {
self.picker = picker
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[.originalImage] as? UIImage else { return }
self.picker.selectedImage = selectedImage
self.picker.isPresented.wrappedValue.dismiss()
}
本文标签: Getting image from camera in SwiftUIStack Overflow
版权声明:本文标题:Getting image from camera in SwiftUI - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744132565a2592244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论