Reputation: 11
I am creating a meme object which consists of a UImage and two TextFields.
I am recreating a Storyboard application with SwiftUI, so I have used the UIViewControllerRepresentable.
Once the completionHandler "is completed" I should be able to save the new image, however the only option that comes up is "Save to Files" which results in only being able to save as a PNG.
My ViewController code is as follows:
struct ShareViewController: UIViewControllerRepresentable {
//the data you need to share....
@Binding var topText: String
@Binding var bottomText: String
@Binding var imagePicked: UIImage?
@Binding var memedImage: UIImage?
func makeUIViewController(context: Context) -> UIActivityViewController {
//define an instance of ActivityViewController
let controller = UIActivityViewController(activityItems: [memedImage], applicationActivities: nil)
//completion handler
controller.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
//create a meme if completed
if completed {
print("HERE")
print(memedImage)
self.saveMeme(topText: self.topText, bottomText: self.bottomText, originalImage: self.imagePicked!, memedImage: self.memedImage!)
}else{
print("error")
}
}
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {}
func saveMeme(topText: String, bottomText: String, originalImage: UIImage, memedImage: UIImage) {
let meme = Meme(topText: topText, bottomText: bottomText, original: originalImage, memed: memedImage)
}
}
//This is the View where I call the controller via .sheet:
struct MemeSwiftUIView: View {
@State private var topText: String = ""
@State private var bottomText: String = ""
@State private var imagePicked: UIImage?
@State private var memedImage: UIImage?
@State private var isShowingPicker = false
@State private var isReadyToShare = false
@State private var sourceType: UIImagePickerController.SourceType = .camera
@State private var shareButtonEnabled = false
var body: some View {
NavigationStack {
features
//top bar
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
//creates a UIGraphicsImageRenderer to create final memedImage
generateMemedImage()
isReadyToShare = true
}){
Image(systemName: "square.and.arrow.up")
}
.disabled(!shareButtonEnabled)
.sheet(isPresented: $isReadyToShare, content: {
//sends the data to ShareViewController:
ShareViewController(topText: $topText, bottomText: $bottomText, imagePicked: $imagePicked, memedImage: $memedImage)
})
}
}
//...rest of struct View
I have already added the necessary info.plist requirements regarding the camera/picture saving features and I have seen that I have a UImage present that is being saved (printed to console): Optional(<UIImage:0x600000bbc630 anonymous {1668, 2500} renderingMode=automatic(original)> Why else would the "save image" option not be available?
Upvotes: 1
Views: 57