I am developing an application as a project for training. This is a meme sharing application. I use .sheet in order to present a modal view to share meme given as an image :
.sheet(isPresented: $showingSharePage) {
ShareSheet(items: items)
}
Here is my code for the share sheet :
struct ShareSheet: UIViewControllerRepresentable {
var items: [Any]
func makeUIViewController(context: Context) -> UIActivityViewController {
let controller = UIActivityViewController(activityItems: items, applicationActivities: nil)
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
}
}
Here is my share button's code :
ToolbarItemGroup {
AnimatedActionButton(title: "Share", systemImage: "square.and.arrow.up") {
items.removeAll()
let snapshot = meme.snapshot()
items.append(snapshot)
showingSharePage = true
}
}
The problem is that when I open for the first time (when application is launched), it doesn't show anything :
After after launching once, it works alright.
Why is it doing this ?
P.S : For further information, I share the whole GitHub project (this is a project for a course from Udacity) : https://github.com/m4thus4n/Project
Post
Replies
Boosts
Views
Activity
I have the following code in my class MemeMeViewModel which is an ObservableObject :
@Published var memes: [MemeMeModel.Meme] = retreiveFromArrayOfMeme()
func retreive() -> [(UIImage, String)] {
var temp = [(UIImage, String)]()
if let Memes = UserDefaults.standard.object(forKey: "MemesList") as? [[Any]] {
for Meme in Memes {
if let image = UIImage(data: Meme[0] as! Data) {
temp.append((image, Meme[1] as! String))
}
}
}
return temp
}
func retreiveFromArrayOfMeme() -> [MemeMeModel.Meme] {
var temp = [MemeMeModel.Meme]()
if let Memes = UserDefaults.standard.object(forKey: "MemesList") as? [[Any]] {
for Meme in Memes {
temp.append(MemeMeModel.Meme(pngRepresentation: Meme[0] as! Data, content: Meme[1] as! String))
}
}
return temp
}
On line 1, I have the following error : "Cannot use instance member 'retreiveFromArrayOfMeme' within property initializer; property initializers run before 'self' is available".
I don't know what to do ... Need help please !
I have two different buttons : Camera button and Album button. When tapping on the album button in order to select an image from photo library, the camera opens. Once I opened the camera by tapping in the camera button, the album works correctly.
I don't know what to do. Here is my code :
struct MemeCreator: View {
@State private var sourceType: UIImagePickerController.SourceType = .camera
@State private var image: Image?
@State private var showingImagePicker = false
@State private var inputImage: UIImage?
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
var body: some View {
NavigationView {
Group {
if let image = image {
Rectangle()
.scaledToFill()
.foregroundColor(.gray)
.overlay(
image
.resizable()
.scaledToFit()
)
} else {
Rectangle()
.scaledToFill()
.foregroundColor(Color(red: 0.15, green: 0.15, blue: 0.15))
}
}
.navigationBarTitle(Text(""), displayMode: .inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(
action: {
}
) {
Image(systemName: "square.and.arrow.up")
.font(.title)
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(
action: {
self.mode.wrappedValue.dismiss()
}
) {
Text("Cancel")
}
}
ToolbarItemGroup(placement: .bottomBar) {
Spacer()
Button(
action: {
sourceType = .camera
showingImagePicker = true
}
) {
Image(systemName: "camera.fill")
}
Spacer()
Button(
action: {
sourceType = .photoLibrary
showingImagePicker = true
}
) {
Text("Album")
}
Spacer()
}
}
}
.sheet(isPresented: $showingImagePicker, onDismiss: loadImage) {
ImagePicker(image: $inputImage, sourceType: sourceType)
}
}
Here is the ImagePicker code :
struct ImagePicker: UIViewControllerRepresentable {
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let parent: ImagePicker
init(_ parent: ImagePicker) {
self.parent = parent
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let uiImage = info[.originalImage] as? UIImage {
parent.image = uiImage
}
parent.presentationMode.wrappedValue.dismiss()
}
}
@Environment(\.presentationMode) var presentationMode
@Binding var image: UIImage?
var sourceType: UIImagePickerController.SourceType
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.sourceType = sourceType
picker.delegate = context.coordinator
return picker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {
}
}