From 2b25d875bcc5cd0313530391ff083352aa9bb9f2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 20 Dec 2018 09:29:15 -0500 Subject: [PATCH 1/3] Don't select a tool by default in image editor view. --- SignalMessaging/Views/ImageEditor/ImageEditorView.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift index 2dfcd4268..67b7ea554 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift @@ -11,21 +11,26 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate { private let model: ImageEditorModel enum EditorMode: String { + case none case brush case crop } - private var editorMode = EditorMode.brush { + private var editorMode = EditorMode.none { didSet { AssertIsOnMainThread() switch editorMode { + case .none: + editorGestureRecognizer?.isEnabled = false case .brush: // Brush strokes can start and end (and return from) outside the view. editorGestureRecognizer?.shouldAllowOutsideView = true + editorGestureRecognizer?.isEnabled = true case .crop: // Crop gestures can start and end (and return from) outside the view. editorGestureRecognizer?.shouldAllowOutsideView = true + editorGestureRecognizer?.isEnabled = true } } } @@ -209,6 +214,8 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate { AssertIsOnMainThread() switch editorMode { + case .none: + break case .brush: handleBrushGesture(gestureRecognizer) case .crop: From b24e8e4f816df868ef2a1049a77510852674838f Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 20 Dec 2018 09:32:48 -0500 Subject: [PATCH 2/3] Use autoreleasepool when rendering image editor output. --- .../ViewControllers/AttachmentApprovalViewController.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift index e84076b8b..4ce2073ec 100644 --- a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift +++ b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift @@ -582,7 +582,11 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC } var attachments: [SignalAttachment] { - return attachmentItems.map { self.processedAttachment(forAttachmentItem: $0) } + return attachmentItems.map { (attachmentItem) in + autoreleasepool { + return self.processedAttachment(forAttachmentItem: attachmentItem) + } + } } // For any attachments edited with the image editor, returns a From a440f692ce3afcdf05fd253fd3dcdf6cd19ffb98 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 20 Dec 2018 09:42:28 -0500 Subject: [PATCH 3/3] Clean up image editor temp files. --- .../Views/ImageEditor/ImageEditorModel.swift | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift b/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift index 5d1b73d2f..94c20d3dc 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift @@ -496,6 +496,36 @@ public class ImageEditorModel: NSObject { }, changedItemIds: [item.itemId]) } + // MARK: - Temp Files + + private var temporaryFilePaths = [String]() + + @objc + public func temporaryFilePath(withFileExtension fileExtension: String) -> String { + AssertIsOnMainThread() + + let filePath = OWSFileSystem.temporaryFilePath(withFileExtension: fileExtension) + temporaryFilePaths.append(filePath) + return filePath + } + + deinit { + AssertIsOnMainThread() + + let temporaryFilePaths = self.temporaryFilePaths + + DispatchQueue.global(qos: .background).async { + for filePath in temporaryFilePaths { + guard OWSFileSystem.deleteFile(filePath) else { + Logger.error("Could not delete temp file: \(filePath)") + continue + } + } + } + } + + // MARK: - Crop + @objc public func crop(unitCropRect: CGRect) { guard let croppedImage = ImageEditorModel.crop(imagePath: contents.imagePath, @@ -510,7 +540,7 @@ public class ImageEditorModel: NSObject { owsFailDebug("Could not convert cropped image to PNG.") return } - let croppedImagePath = OWSFileSystem.temporaryFilePath(withFileExtension: "png") + let croppedImagePath = temporaryFilePath(withFileExtension: "png") do { try croppedImageData.write(to: NSURL.fileURL(withPath: croppedImagePath), options: .atomicWrite) } catch let error as NSError {