More photo upload fixes

This commit is contained in:
Ilya Laktyushin
2023-04-24 00:07:11 +04:00
parent 2967795424
commit 060bc61011
3 changed files with 212 additions and 228 deletions

View File

@@ -476,9 +476,19 @@ public class CheckLayer: CALayer {
context.strokePath()
case let .counter(number):
let text = NSAttributedString(string: "\(number)", font: Font.with(size: 16.0, design: .round, weight: .regular, traits: []), textColor: parameters.theme.strokeColor.withMultipliedAlpha(parameters.animationProgress))
let fontSize: CGFloat
let string = "\(number)"
switch string.count {
case 1:
fontSize = 16.0
case 2:
fontSize = 15.0
default:
fontSize = 13.0
}
let text = NSAttributedString(string: string, font: Font.with(size: fontSize, design: .round, weight: .medium, traits: []), textColor: parameters.theme.strokeColor.withMultipliedAlpha(parameters.animationProgress))
let textRect = text.boundingRect(with: CGSize(width: 100.0, height: 100.0), options: [.usesLineFragmentOrigin], context: nil)
text.draw(at: CGPoint(x: UIScreenPixel + textRect.minX + floor((size.width - textRect.width) * 0.5), y: textRect.minY + floorToScreenPixels((size.height - textRect.height) * 0.5)))
text.draw(at: CGPoint(x: textRect.minX + floorToScreenPixels((size.width - textRect.width) * 0.5), y: textRect.minY + floorToScreenPixels((size.height - textRect.height) * 0.5)))
}
}
}

View File

@@ -83,29 +83,25 @@ extension UIImage.Orientation {
}
private let fetchPhotoWorkers = ThreadPool(threadCount: 3, threadPriority: 0.2)
private let fetchPhotoQueue = ThreadPoolQueue(threadPool: fetchPhotoWorkers)
public func fetchPhotoLibraryResource(localIdentifier: String) -> Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> {
return Signal { subscriber in
let queue = ThreadPoolQueue(threadPool: fetchPhotoWorkers)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: nil)
let requestId = Atomic<RequestId>(value: RequestId())
if fetchResult.count != 0 {
let asset = fetchResult.object(at: 0)
let option = PHImageRequestOptions()
option.deliveryMode = .opportunistic
option.deliveryMode = .highQualityFormat
option.isNetworkAccessAllowed = true
option.isSynchronous = false
let madeProgress = Atomic<Bool>(value: false)
option.progressHandler = { progress, error, _, _ in
if !madeProgress.swap(true) {
//subscriber.putNext(.reset)
}
}
let size = CGSize(width: 1280.0, height: 1280.0)
queue.addTask(ThreadPoolTask({ _ in
let startTime = CACurrentMediaTime()
fetchPhotoQueue.addTask(ThreadPoolTask({ _ in
let semaphore = DispatchSemaphore(value: 0)
let requestIdValue = PHImageManager.default().requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: option, resultHandler: { (image, info) -> Void in
Queue.concurrentDefaultQueue().async {
@@ -117,16 +113,12 @@ public func fetchPhotoLibraryResource(localIdentifier: String) -> Signal<MediaRe
}
if let image = image {
if let info = info, let degraded = info[PHImageResultIsDegradedKey], (degraded as AnyObject).boolValue!{
if !madeProgress.swap(true) {
//subscriber.putNext(.reset)
}
} else {
#if DEBUG
print("load completion \((CACurrentMediaTime() - startTime) * 1000.0) ms")
#endif
_ = madeProgress.swap(true)
let scale = min(1.0, min(size.width / max(1.0, image.size.width), size.height / max(1.0, image.size.height)))
let scaledSize = CGSize(width: floor(image.size.width * scale), height: floor(image.size.height * scale))
let scaledImage = resizedImage(image, for: scaledSize)
@@ -144,14 +136,12 @@ public func fetchPhotoLibraryResource(localIdentifier: String) -> Signal<MediaRe
} else {
subscriber.putCompletion()
}
semaphore.signal()
}
} else {
if !madeProgress.swap(true) {
//subscriber.putNext(.reset)
}
}
semaphore.signal()
}
}
})
requestId.with { current -> Void in
if !current.invalidated {

View File

@@ -207,13 +207,6 @@ private final class FetchVideoLibraryMediaResourceContext {
private let throttlingContext = FetchVideoLibraryMediaResourceContext()
public func fetchVideoLibraryMediaResource(account: Account, resource: VideoLibraryMediaResource) -> Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> {
return account.postbox.preferencesView(keys: [PreferencesKeys.appConfiguration])
|> take(1)
|> map { view in
return view.values[PreferencesKeys.appConfiguration]?.get(AppConfiguration.self) ?? .defaultValue
}
|> castError(MediaResourceDataFetchError.self)
|> mapToSignal { appConfiguration -> Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> in
let signal = Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> { subscriber in
subscriber.putNext(.reset)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [resource.localIdentifier], options: nil)
@@ -319,17 +312,9 @@ public func fetchVideoLibraryMediaResource(account: Account, resource: VideoLibr
}
}
return throttlingContext.wrap(priority: .default, signal: signal)
}
}
func fetchLocalFileVideoMediaResource(account: Account, resource: LocalFileVideoMediaResource) -> Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> {
return account.postbox.preferencesView(keys: [PreferencesKeys.appConfiguration])
|> take(1)
|> map { view in
return view.values[PreferencesKeys.appConfiguration]?.get(AppConfiguration.self) ?? .defaultValue
}
|> castError(MediaResourceDataFetchError.self)
|> mapToSignal { appConfiguration -> Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> in
let signal = Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> { subscriber in
subscriber.putNext(.reset)
@@ -452,7 +437,6 @@ func fetchLocalFileVideoMediaResource(account: Account, resource: LocalFileVideo
}
}
return throttlingContext.wrap(priority: .default, signal: signal)
}
}
public func fetchVideoLibraryMediaResourceHash(resource: VideoLibraryMediaResource) -> Signal<Data?, NoError> {