Skip to content

Commit

Permalink
improve sharing large images (#2020)
Browse files Browse the repository at this point in the history
* log errors as such

* skip unneeded nil assignment

* prescale images to fit into extension restrictions

extensions must not use more than 120mb of memory,
this is easily exhausted by core scaling down images.

using iOS function for scaling use less memory (or the memory is not counted)
and avoids this error.

although we accept various formats of images,
in practise i've seen them passing to the extension only as URL,
if we see that for other formats as well, we can optimize the other branches,
currently, that seems not to be needed.
  • Loading branch information
r10s authored Dec 15, 2023
1 parent f339046 commit 7869d32
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions DcShare/Helper/ShareAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ShareAttachment {
case let url as URL:
result = SDAnimatedImage(contentsOfFile: url.path)
default:
logger.debug("Unexpected data: \(type(of: data))")
logger.error("Unexpected data: \(type(of: data))")
}
if let result = result {
let path = ImageFormat.saveImage(image: result, directory: .cachesDirectory)
Expand Down Expand Up @@ -108,10 +108,12 @@ class ShareAttachment {
case let data as Data:
result = ImageFormat.loadImageFrom(data: data)
case let url as URL:
result = ImageFormat.loadImageFrom(url: url)
if let nsurl = NSURL(string: url.absoluteString) {
// scaleDownImage() uses less memory than core and avoids exhausing the 120 mb memory restriction of extensions (see #1330)
result = ImageFormat.scaleDownImage(nsurl, toMax: 1280)
}
default:
logger.debug("Unexpected data: \(type(of: data))")
result = nil
logger.error("Unexpected data: \(type(of: data))")
}
if let result = result,
let path = ImageFormat.saveImage(image: result, directory: .cachesDirectory) {
Expand Down Expand Up @@ -146,7 +148,7 @@ class ShareAttachment {

}
default:
logger.debug("Unexpected data: \(type(of: data))")
logger.error("Unexpected data: \(type(of: data))")
}
if let error = error {
logger.error("Could not load share item as video: \(error.localizedDescription)")
Expand Down Expand Up @@ -182,7 +184,7 @@ class ShareAttachment {
self.generateThumbnailRepresentations(url: url)
}
default:
logger.debug("Unexpected data: \(type(of: data))")
logger.error("Unexpected data: \(type(of: data))")
}
if let error = error {
logger.error("Could not load share item: \(error.localizedDescription)")
Expand Down Expand Up @@ -231,7 +233,7 @@ class ShareAttachment {
case let url as URL:
delegate.onUrlShared(url: url)
default:
logger.debug("Unexpected data: \(type(of: data))")
logger.error("Unexpected data: \(type(of: data))")
}
if let error = error {
logger.error("Could not share URL: \(error.localizedDescription)")
Expand Down

0 comments on commit 7869d32

Please sign in to comment.