Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds cache to store expensive frame data #83

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Classes/Archival/Archive.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//

/// The class containing an image or video and associated data (an encoded representation of the edits).
class Archive: NSObject, NSSecureCoding {
static var supportsSecureCoding: Bool = true

let image: UIImage?
let video: URL?
let data: Data?

init(image: UIImage, data: Data?) {
self.image = image
self.data = data
self.video = nil
}

init(video: URL, data: Data?) {
self.video = video
self.data = data
self.image = nil
}

private enum CodingKeys: String {
case image
case video
case data
}

func encode(with coder: NSCoder) {
coder.encode(image, forKey: CodingKeys.image.rawValue)
coder.encode(video?.absoluteString, forKey: CodingKeys.video.rawValue)
coder.encode(data?.base64EncodedString(), forKey: CodingKeys.data.rawValue)
}

required init?(coder: NSCoder) {
image = coder.decodeObject(of: UIImage.self, forKey: CodingKeys.image.rawValue)
if let urlString = coder.decodeObject(of: NSString.self, forKey: CodingKeys.video.rawValue) as String? {
video = URL(string: urlString)
} else {
video = nil
}
if let dataString = coder.decodeObject(of: NSString.self, forKey: CodingKeys.data.rawValue) as String? {
data = Data(base64Encoded: dataString)
} else {
data = nil
}
}
}
188 changes: 130 additions & 58 deletions Classes/Camera/CameraController.swift

Large diffs are not rendered by default.

35 changes: 32 additions & 3 deletions Classes/Camera/MediaArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,50 @@ class MediaArchiver {
} else {
originalURL = nil
}
return KanvasMedia(image: image, url: url, original: originalURL, info: export.info)
let archiveURL = self.archive(media: .image(original), archive: export.archive, to: url.deletingPathExtension().lastPathComponent)
return KanvasMedia(image: image, url: url, original: originalURL, info: export.info, archive: archiveURL)
} else {
return nil
}
case (.video(let url), .video(let original)):
let archiveURL = self.archive(media: .video(original), archive: export.archive, to: url.deletingPathExtension().lastPathComponent)
os_log(.debug, log: log, "Original video URL: %@", original.absoluteString)
let asset = AVURLAsset(url: url)
return KanvasMedia(asset: asset, original: original, info: export.info)
return KanvasMedia(asset: asset, original: original, info: export.info, archive: archiveURL)
default:
return nil
}
}

private func archive(media: EditorViewController.Media, archive data: Data, to path: String) -> URL? {

let archive: Archive

switch media {
case .image(let image):
archive = Archive(image: image, data: data)
case .video(let url):
archive = Archive(video: url, data: data)
}

let archiveURL: URL?
if let saveDirectory = saveDirectory {
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: archive, requiringSecureCoding: true)
archiveURL = try data.save(to: path, in: saveDirectory, ext: "")
} catch let error {
archiveURL = nil
print("Failed to archive \(error)")
}
} else {
archiveURL = nil
}

return archiveURL
}
}

private extension Data {
extension Data {
func save(to filename: String, in directory: URL, ext fileExtension: String) throws -> URL {
let fileURL = directory.appendingPathComponent(filename).appendingPathExtension(fileExtension)
try write(to: fileURL, options: .atomic)
Expand Down
16 changes: 8 additions & 8 deletions Classes/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,7 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
)
}()

lazy var movableViewCanvas: MovableViewCanvas = {
let canvas = MovableViewCanvas()
canvas.delegate = self
return canvas
}()
var movableViewCanvas: MovableViewCanvas

private lazy var movableViewCanvasConstraints = {
return FullViewConstraints(
Expand Down Expand Up @@ -219,7 +215,8 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
showBlogSwitcher: Bool,
quickBlogSelectorCoordinator: KanvasQuickBlogSelectorCoordinating?,
tagCollection: UIView?,
metalContext: MetalContext?) {
metalContext: MetalContext?,
movableViewCanvas: MovableViewCanvas?) {
self.delegate = delegate
self.mainActionMode = mainActionMode
self.showSaveButton = showSaveButton
Expand All @@ -232,7 +229,10 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
self.quickBlogSelectorCoordinator = quickBlogSelectorCoordinator
self.tagCollection = tagCollection
self.metalContext = metalContext
self.movableViewCanvas = movableViewCanvas ?? MovableViewCanvas()

super.init(frame: .zero)
self.movableViewCanvas.delegate = self
setupViews()
}

Expand Down Expand Up @@ -277,7 +277,7 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
setupOverlay()
setupOverlayLabel()
}

// MARK: - views

private func setupPlayer() {
Expand All @@ -299,7 +299,7 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
addSubview(movableViewCanvas)
movableViewCanvasConstraints.activate()
}

/// Container that holds the back button and the bottom menu
private func setupNavigationContainer() {
navigationContainer.accessibilityIdentifier = "Navigation Container"
Expand Down
Loading