2026-01-24 14:34:29 -05:00
|
|
|
import Accelerate
|
|
|
|
|
import Flutter
|
2026-08-10 21:14:23 +06:00
|
|
|
import ImageIO
|
2026-01-24 14:34:29 -05:00
|
|
|
import MobileCoreServices
|
|
|
|
|
import Photos
|
|
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
final class RemoteImageRequest: ImageRequest {
|
|
|
|
|
var task: URLSessionDataTask?
|
2026-01-24 14:34:29 -05:00
|
|
|
let id: Int64
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
init(id: Int64, completion: @escaping @Sendable (Result<[String: Int64]?, any Error>) -> Void) {
|
2026-01-24 14:34:29 -05:00
|
|
|
self.id = id
|
2026-04-10 17:56:35 +02:00
|
|
|
super.init(completion: completion)
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
2026-04-06 00:55:43 +02:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
override func cancel() {
|
|
|
|
|
super.cancel()
|
2026-04-06 00:55:43 +02:00
|
|
|
task?.cancel()
|
|
|
|
|
}
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RemoteImageApiImpl: NSObject, RemoteImageApi {
|
2026-04-06 00:55:43 +02:00
|
|
|
private static let registry = RequestRegistry<RemoteImageRequest>()
|
2026-04-10 17:56:35 +02:00
|
|
|
private static let rgbaFormat = vImage_CGImageFormat(
|
2026-01-24 14:34:29 -05:00
|
|
|
bitsPerComponent: 8,
|
|
|
|
|
bitsPerPixel: 32,
|
|
|
|
|
colorSpace: CGColorSpaceCreateDeviceRGB(),
|
|
|
|
|
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue),
|
|
|
|
|
renderingIntent: .perceptual
|
|
|
|
|
)!
|
2026-08-10 21:14:23 +06:00
|
|
|
private static let decodeOptions: [NSString: Bool] = [
|
2026-01-24 14:34:29 -05:00
|
|
|
kCGImageSourceShouldCache: false,
|
|
|
|
|
kCGImageSourceShouldCacheImmediately: true,
|
|
|
|
|
kCGImageSourceCreateThumbnailWithTransform: true,
|
2026-01-28 16:14:37 -05:00
|
|
|
kCGImageSourceCreateThumbnailFromImageAlways: true
|
2026-08-10 21:14:23 +06:00
|
|
|
]
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-08-10 21:14:23 +06:00
|
|
|
func requestImage(url: String, requestId: Int64, preferEncoded: Bool, width: Int64?, height: Int64?, completion: @escaping (Result<[String : Int64]?, any Error>) -> Void) {
|
2026-02-05 12:42:53 -05:00
|
|
|
var urlRequest = URLRequest(url: URL(string: url)!)
|
|
|
|
|
urlRequest.cachePolicy = .returnCacheDataElseLoad
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
let request = RemoteImageRequest(id: requestId, completion: completion)
|
|
|
|
|
|
2026-02-05 12:42:53 -05:00
|
|
|
let task = URLSessionManager.shared.session.dataTask(with: urlRequest) { data, response, error in
|
2026-08-10 21:14:23 +06:00
|
|
|
Self.handleCompletion(request: request, encoded: preferEncoded, width: width, height: height, data: data, response: response, error: error)
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
request.task = task
|
2026-04-06 00:55:43 +02:00
|
|
|
Self.registry.add(requestId: requestId, request: request)
|
2026-02-05 12:42:53 -05:00
|
|
|
task.resume()
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-08-10 21:14:23 +06:00
|
|
|
private static func handleCompletion(request: RemoteImageRequest, encoded: Bool, width: Int64?, height: Int64?, data: Data?, response: URLResponse?, error: Error?) {
|
2026-04-10 17:56:35 +02:00
|
|
|
if request.isCancelled {
|
|
|
|
|
return request.completion(ImageProcessing.cancelledResult)
|
2026-02-05 12:42:53 -05:00
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-01-24 14:34:29 -05:00
|
|
|
if let error = error {
|
2026-04-10 17:56:35 +02:00
|
|
|
registry.remove(requestId: request.id)
|
2026-01-24 14:34:29 -05:00
|
|
|
return request.completion(.failure(error))
|
|
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-02-05 12:42:53 -05:00
|
|
|
guard let data = data else {
|
2026-04-10 17:56:35 +02:00
|
|
|
registry.remove(requestId: request.id)
|
2026-01-24 14:34:29 -05:00
|
|
|
return request.completion(.failure(PigeonError(code: "", message: "No data received", details: nil)))
|
|
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
if encoded {
|
|
|
|
|
let length = data.count
|
|
|
|
|
let pointer = malloc(length)!
|
|
|
|
|
data.copyBytes(to: pointer.assumingMemoryBound(to: UInt8.self), count: length)
|
|
|
|
|
|
2026-01-24 14:34:29 -05:00
|
|
|
if request.isCancelled {
|
2026-04-10 17:56:35 +02:00
|
|
|
free(pointer)
|
2026-02-05 12:42:53 -05:00
|
|
|
return request.completion(ImageProcessing.cancelledResult)
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
registry.remove(requestId: request.id)
|
|
|
|
|
return request.completion(
|
|
|
|
|
.success([
|
|
|
|
|
"pointer": Int64(Int(bitPattern: pointer)),
|
|
|
|
|
"length": Int64(length),
|
|
|
|
|
]))
|
|
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
ImageProcessing.queue.addOperation {
|
|
|
|
|
if request.isCancelled {
|
|
|
|
|
return request.completion(ImageProcessing.cancelledResult)
|
2026-02-28 17:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-08-10 21:14:23 +06:00
|
|
|
guard let imageSource = CGImageSourceCreateWithData(data as CFData, nil) else {
|
|
|
|
|
registry.remove(requestId: request.id)
|
|
|
|
|
return request.completion(.failure(PigeonError(code: "", message: "Failed to decode image for request", details: nil)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var options: [NSString: Any] = decodeOptions
|
|
|
|
|
if let maxPixelSize = targetThumbnailRenderSize(imageSource: imageSource, width: width, height: height) {
|
|
|
|
|
options[kCGImageSourceThumbnailMaxPixelSize] = maxPixelSize
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard let cgImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary) else {
|
2026-04-10 17:56:35 +02:00
|
|
|
registry.remove(requestId: request.id)
|
2026-02-05 12:42:53 -05:00
|
|
|
return request.completion(.failure(PigeonError(code: "", message: "Failed to decode image for request", details: nil)))
|
|
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-02-05 12:42:53 -05:00
|
|
|
if request.isCancelled {
|
|
|
|
|
return request.completion(ImageProcessing.cancelledResult)
|
|
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-02-05 12:42:53 -05:00
|
|
|
do {
|
|
|
|
|
let buffer = try vImage_Buffer(cgImage: cgImage, format: rgbaFormat)
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-02-05 12:42:53 -05:00
|
|
|
if request.isCancelled {
|
|
|
|
|
buffer.free()
|
|
|
|
|
return request.completion(ImageProcessing.cancelledResult)
|
|
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-04-10 17:56:35 +02:00
|
|
|
registry.remove(requestId: request.id)
|
|
|
|
|
return request.completion(
|
|
|
|
|
.success([
|
|
|
|
|
"pointer": Int64(Int(bitPattern: buffer.data)),
|
|
|
|
|
"width": Int64(buffer.width),
|
|
|
|
|
"height": Int64(buffer.height),
|
|
|
|
|
"rowBytes": Int64(buffer.rowBytes),
|
|
|
|
|
]))
|
2026-02-05 12:42:53 -05:00
|
|
|
} catch {
|
2026-04-10 17:56:35 +02:00
|
|
|
registry.remove(requestId: request.id)
|
2026-02-05 12:42:53 -05:00
|
|
|
return request.completion(.failure(PigeonError(code: "", message: "Failed to convert image for request: \(error)", details: nil)))
|
|
|
|
|
}
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-08-10 21:14:23 +06:00
|
|
|
/// Returns the longest rendered edge needed to cover the requested size.
|
|
|
|
|
private static func targetThumbnailRenderSize(imageSource: CGImageSource, width: Int64?, height: Int64?) -> Int? {
|
|
|
|
|
guard let width,
|
|
|
|
|
let height,
|
|
|
|
|
width > 0,
|
|
|
|
|
height > 0,
|
|
|
|
|
let properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [CFString: Any],
|
|
|
|
|
let pixelWidth = properties[kCGImagePropertyPixelWidth] as? NSNumber,
|
|
|
|
|
let pixelHeight = properties[kCGImagePropertyPixelHeight] as? NSNumber else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let orientation = (properties[kCGImagePropertyOrientation] as? NSNumber)
|
|
|
|
|
.flatMap { CGImagePropertyOrientation(rawValue: $0.uint32Value) } ?? .up
|
|
|
|
|
let swapsDimensions: Bool
|
|
|
|
|
switch orientation {
|
|
|
|
|
case .leftMirrored, .right, .rightMirrored, .left:
|
|
|
|
|
swapsDimensions = true
|
|
|
|
|
default:
|
|
|
|
|
swapsDimensions = false
|
|
|
|
|
}
|
|
|
|
|
let sourceWidth = swapsDimensions ? pixelHeight.doubleValue : pixelWidth.doubleValue
|
|
|
|
|
let sourceHeight = swapsDimensions ? pixelWidth.doubleValue : pixelHeight.doubleValue
|
|
|
|
|
let fillScale = max(Double(width) / sourceWidth, Double(height) / sourceHeight)
|
|
|
|
|
let scale = min(1, fillScale)
|
|
|
|
|
return scale < 1 ? Int(ceil(max(sourceWidth * scale, sourceHeight * scale))) : nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-05 12:42:53 -05:00
|
|
|
func cancelRequest(requestId: Int64) {
|
2026-04-06 00:55:43 +02:00
|
|
|
Self.registry.remove(requestId: requestId)?.cancel()
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
2026-02-28 17:43:58 +01:00
|
|
|
|
2026-02-05 12:42:53 -05:00
|
|
|
func clearCache(completion: @escaping (Result<Int64, any Error>) -> Void) {
|
|
|
|
|
Task {
|
|
|
|
|
let cache = URLSessionManager.shared.session.configuration.urlCache!
|
|
|
|
|
let cacheSize = Int64(cache.currentDiskUsage)
|
|
|
|
|
cache.removeAllCachedResponses()
|
|
|
|
|
completion(.success(cacheSize))
|
2026-01-24 14:34:29 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|