Files
session-ios/Session/Settings/ImagePickerHandler.swift
Morgan Pretty 90d13cba83 Fixed a number of issues found when debugging image orientation bug
• Added the ability to set the custom file server via env variables for testing
• Disable image editing on the share extension if there isn't enough RAM available to edit the image (ie. prevent crashes due to insufficient memory)
• Tweaked image loading to avoid pre-decoding large static images to reduce memory spikes (was crashing the share extension on some devices)
• Updated link previews to load images using the ImageDataManager
• Fixed an issue where the file id on the message info screen would include extra info
• Fixed an issue where sharing a file wouldn't show the preview correctly
• Fixed an issue where the re-upload job could run when a display picture file was missing
• Fixed an issue where deleting messages across all devices in Note to Self was using the wrong deletion function
• Fixed an issue where an invalid database value could result in no attachments being able to be loaded in a conversation
• Fixed an issue where images with orientation metadata would be incorrectly rotated and letterboxed
2025-10-29 16:52:23 +11:00

78 lines
3.2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import UIKit
import PhotosUI
import UniformTypeIdentifiers
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
class ImagePickerHandler: PHPickerViewControllerDelegate {
private let dependencies: Dependencies
private let onTransition: (UIViewController, TransitionType) -> Void
private let onImagePicked: (ImageDataManager.DataSource, CGRect?) -> Void
// MARK: - Initialization
init(
onTransition: @escaping (UIViewController, TransitionType) -> Void,
onImagePicked: @escaping (ImageDataManager.DataSource, CGRect?) -> Void,
using dependencies: Dependencies
) {
self.dependencies = dependencies
self.onTransition = onTransition
self.onImagePicked = onImagePicked
}
// MARK: - UIImagePickerControllerDelegate
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
guard
let result: PHPickerResult = results.first,
let typeIdentifier: String = result.itemProvider.registeredTypeIdentifiers.first
else {
picker.dismiss(animated: true)
return
}
picker.dismiss(animated: true) { [weak self] in
result.itemProvider.loadFileRepresentation(forTypeIdentifier: typeIdentifier) { url, error in
guard let self = self else { return }
guard let url: URL = url else {
Log.debug("[ImagePickHandler] Error loading file: \(error?.localizedDescription ?? "unknown")")
return
}
do {
let onImagePicked: (ImageDataManager.DataSource, CGRect?) -> Void = self.onImagePicked
let filePath: String = self.dependencies[singleton: .fileManager].temporaryFilePath()
try self.dependencies[singleton: .fileManager].copyItem(
atPath: url.path,
toPath: filePath
)
// TODO: Need to remove file when we are done
DispatchQueue.main.async { [weak self, dataManager = self.dependencies[singleton: .imageDataManager]] in
let viewController: CropScaleImageViewController = CropScaleImageViewController(
source: .url(URL(fileURLWithPath: filePath)),
dstSizePixels: CGSize(
width: DisplayPictureManager.maxDimension,
height: DisplayPictureManager.maxDimension
),
dataManager: dataManager,
successCompletion: onImagePicked
)
self?.onTransition(
StyledNavigationController(rootViewController: viewController),
.present
)
}
}
catch {
Log.debug("[ImagePickHandler] Error copying file: \(error)")
}
}
}
}
}