Skip to content

Instantly share code, notes, and snippets.

@NickG-DK
Created February 20, 2021 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickG-DK/860af99598f9fdee002f5385168b30c8 to your computer and use it in GitHub Desktop.
Save NickG-DK/860af99598f9fdee002f5385168b30c8 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ContentView: View {
@State var showActionSheet = false
@State var showImagePicker = false
@State var sourceType:UIImagePickerController.SourceType = .camera
@State var image:UIImage?
var body: some View {
VStack {
if image != nil {
Image(uiImage: image!)
.resizable()
.scaledToFit()
.frame(width:150, height:150)
} else {
Image(systemName: "person")
.resizable()
.scaledToFit()
.frame(width:150, height:150)
}
Button(action: {
self.showActionSheet = true
}) {
Text("Show Image Picker")
}.actionSheet(isPresented: $showActionSheet){
ActionSheet(title: Text("Add a picture to your post"), message: nil, buttons: [
//Button1
.default(Text("Camera"), action: {
self.showImagePicker = true
self.sourceType = .camera
}),
//Button2
.default(Text("Photo Library"), action: {
self.showImagePicker = true
self.sourceType = .photoLibrary
}),
//Button3
.cancel()
])
}.sheet(isPresented: $showImagePicker){
imagePicker(image: self.$image, showImagePicker: self.$showImagePicker, sourceType: self.sourceType)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment