Created
May 2, 2020 17:52
-
-
Save karthironald/219418d677a49560449f605fa11722fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// SUImagePickerView | |
// | |
// Created by Karthick Selvaraj on 02/05/20. | |
// Copyright © 2020 Karthick Selvaraj. All rights reserved. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@State private var image: Image? = Image("karthick") | |
@State private var shouldPresentImagePicker = false | |
@State private var shouldPresentActionScheet = false | |
@State private var shouldPresentCamera = false | |
var body: some View { | |
// WARNING: Force wrapped image for demo purpose | |
image! | |
.resizable() | |
.aspectRatio(contentMode: .fill) | |
.frame(width: 300, height: 300) | |
.clipShape(Circle()) | |
.overlay(Circle().stroke(Color.white, 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()]) | |
} | |
} | |
} | |
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