This commit is contained in:
mertalev 2025-07-21 13:52:21 +03:00
parent 4d4a5b6be4
commit 91a5989929
No known key found for this signature in database
GPG Key ID: DF6ABC77AAD98C95

View File

@ -25,45 +25,31 @@ class ThumbnailApiImpl: ThumbnailApi {
guard let bufferPointer = UnsafeMutableRawPointer(bitPattern: Int(pointer)) guard let bufferPointer = UnsafeMutableRawPointer(bitPattern: Int(pointer))
else { completion(.failure(PigeonError(code: "", message: "Could not get buffer pointer for \(assetId)", details: nil))); return } else { completion(.failure(PigeonError(code: "", message: "Could not get buffer pointer for \(assetId)", details: nil))); return }
Self.processingQueue.async { Self.processingQueue.async {
do { guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: Self.fetchOptions).firstObject
let asset = try self.getAsset(assetId: assetId) else { completion(.failure(PigeonError(code: "", message: "Could not get asset data for \(assetId)", details: nil))); return }
Self.cacheManager.requestImage( Self.cacheManager.requestImage(
for: asset, for: asset,
targetSize: CGSize(width: Double(width), height: Double(height)), targetSize: CGSize(width: Double(width), height: Double(height)),
contentMode: .aspectFill, contentMode: .aspectFill,
options: Self.requestOptions, options: Self.requestOptions,
resultHandler: { (image, info) -> Void in resultHandler: { (image, info) -> Void in
guard let image = image, guard let image = image,
let cgImage = image.cgImage, let cgImage = image.cgImage,
let dataProvider = cgImage.dataProvider, let dataProvider = cgImage.dataProvider,
let pixelData = dataProvider.data let pixelData = dataProvider.data
else { completion(.failure(PigeonError(code: "", message: "Could not get pixel data for \(assetId)", details: nil))); return } else { completion(.failure(PigeonError(code: "", message: "Could not get pixel data for \(assetId)", details: nil))); return }
guard let sourceBuffer = CFDataGetBytePtr(pixelData) guard let sourceBuffer = CFDataGetBytePtr(pixelData)
else { completion(.failure(PigeonError(code: "", message: "Could not get pixel data buffer for \(assetId)", details: nil))); return } else { completion(.failure(PigeonError(code: "", message: "Could not get pixel data buffer for \(assetId)", details: nil))); return }
let dataLength = CFDataGetLength(pixelData) let dataLength = CFDataGetLength(pixelData)
let bufferLength = width * height * 4 let bufferLength = width * height * 4
guard dataLength <= bufferLength guard dataLength <= bufferLength
else { completion(.failure(PigeonError(code: "", message: "Buffer is not large enough (\(bufferLength) vs \(dataLength) for \(assetId)", details: nil))); return } else { completion(.failure(PigeonError(code: "", message: "Buffer is not large enough (\(bufferLength) vs \(dataLength) for \(assetId)", details: nil))); return }
bufferPointer.copyMemory(from: sourceBuffer, byteCount: dataLength) bufferPointer.copyMemory(from: sourceBuffer, byteCount: dataLength)
completion(.success(())) completion(.success(()))
} }
) )
} catch {
completion(
.failure(PigeonError(code: "", message: "Could not get asset data for \(assetId)", details: nil)))
}
} }
} }
private func getAsset(assetId: String) throws -> PHAsset {
guard
let asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: Self.fetchOptions)
.firstObject
else {
throw PigeonError(code: "", message: "Could not fetch asset", details: nil)
}
return asset
}
} }