mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-04-03 15:46:18 +00:00
ios: tidy up tmp images (#494)
* catch image URL and tidy up after the fact Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
This commit is contained in:
@@ -12,6 +12,7 @@ struct ImagePicker: UIViewControllerRepresentable {
|
||||
@Environment(\.presentationMode) var presentationMode
|
||||
var source: UIImagePickerController.SourceType
|
||||
@Binding var image: UIImage?
|
||||
@Binding var imageUrl: URL?
|
||||
|
||||
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
|
||||
let parent: ImagePicker
|
||||
@@ -23,6 +24,7 @@ struct ImagePicker: UIViewControllerRepresentable {
|
||||
func imagePickerController(_ picker: UIImagePickerController,
|
||||
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
|
||||
if let uiImage = info[.originalImage] as? UIImage {
|
||||
parent.imageUrl = info[.imageURL] as? URL
|
||||
parent.image = uiImage
|
||||
}
|
||||
parent.presentationMode.wrappedValue.dismiss()
|
||||
|
||||
@@ -16,6 +16,7 @@ struct UserProfile: View {
|
||||
@State private var showImagePicker = false
|
||||
@State private var imageSource: UIImagePickerController.SourceType = .photoLibrary
|
||||
@State private var pickedImage: UIImage? = nil
|
||||
@State private var tmpImageUrl: URL? = nil
|
||||
|
||||
var body: some View {
|
||||
let user: User = chatModel.currentUser!
|
||||
@@ -88,7 +89,7 @@ struct UserProfile: View {
|
||||
}
|
||||
}
|
||||
.sheet(isPresented: $showImagePicker) {
|
||||
ImagePicker(source: imageSource, image: $pickedImage)
|
||||
ImagePicker(source: imageSource, image: $pickedImage, imageUrl: $tmpImageUrl)
|
||||
}
|
||||
.onChange(of: pickedImage) { image in
|
||||
if let image = image,
|
||||
@@ -99,6 +100,13 @@ struct UserProfile: View {
|
||||
} else {
|
||||
logger.error("UserProfile: resized image is too big \(imageStr.count)")
|
||||
}
|
||||
if let tmpImageUrl = tmpImageUrl {
|
||||
do {
|
||||
try FileManager.default.removeItem(at: tmpImageUrl)
|
||||
} catch {
|
||||
logger.error("UserProfile: file deletion error \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
profile.image = nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user