forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFetchPhotoLibraryImageResource.swift
151 lines (141 loc) · 6.43 KB
/
FetchPhotoLibraryImageResource.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Foundation
import UIKit
import Photos
import Postbox
import SwiftSignalKit
import ImageCompression
private final class RequestId {
var id: PHImageRequestID?
var invalidated: Bool = false
}
public func fetchPhotoLibraryResource(localIdentifier: String) -> Signal<MediaResourceDataFetchResult, MediaResourceDataFetchError> {
return Signal { subscriber in
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.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)
let requestIdValue = PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFit, options: option, resultHandler: { (image, info) -> Void in
Queue.concurrentDefaultQueue().async {
requestId.with { current -> Void in
if !current.invalidated {
current.id = nil
current.invalidated = true
}
}
if let image = image {
if let info = info, let degraded = info[PHImageResultIsDegradedKey], (degraded as AnyObject).boolValue!{
if !madeProgress.swap(true) {
//subscriber.putNext(.reset)
}
} else {
_ = 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))
UIGraphicsBeginImageContextWithOptions(scaledSize, true, 1.0)
image.draw(in: CGRect(origin: CGPoint(), size: scaledSize))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
if let scaledImage = scaledImage, let data = compressImageToJPEG(scaledImage, quality: 0.6) {
subscriber.putNext(.dataPart(resourceOffset: 0, data: data, range: 0 ..< data.count, complete: true))
subscriber.putCompletion()
} else {
subscriber.putCompletion()
}
}
} else {
if !madeProgress.swap(true) {
//subscriber.putNext(.reset)
}
}
}
})
requestId.with { current -> Void in
if !current.invalidated {
current.id = requestIdValue
}
}
} else {
subscriber.putNext(.reset)
}
return ActionDisposable {
let requestIdValue = requestId.with { current -> PHImageRequestID? in
if !current.invalidated {
let value = current.id
current.id = nil
current.invalidated = true
return value
} else {
return nil
}
}
if let requestIdValue = requestIdValue {
PHImageManager.default().cancelImageRequest(requestIdValue)
}
}
}
}
public func fetchPhotoLibraryImage(localIdentifier: String, thumbnail: Bool) -> Signal<(UIImage, Bool)?, NoError> {
return Signal { subscriber in
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 = .highQualityFormat
if thumbnail {
option.resizeMode = .fast
}
option.isNetworkAccessAllowed = true
option.isSynchronous = false
let targetSize: CGSize = thumbnail ? CGSize(width: 128.0, height: 128.0) : PHImageManagerMaximumSize
let requestIdValue = PHImageManager.default().requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: option, resultHandler: { (image, info) -> Void in
Queue.concurrentDefaultQueue().async {
requestId.with { current -> Void in
if !current.invalidated {
current.id = nil
current.invalidated = true
}
}
if let image = image {
subscriber.putNext((image, thumbnail))
subscriber.putCompletion()
}
}
})
requestId.with { current -> Void in
if !current.invalidated {
current.id = requestIdValue
}
}
} else {
subscriber.putNext(nil)
subscriber.putCompletion()
}
return ActionDisposable {
let requestIdValue = requestId.with { current -> PHImageRequestID? in
if !current.invalidated {
let value = current.id
current.id = nil
current.invalidated = true
return value
} else {
return nil
}
}
if let requestIdValue = requestIdValue {
PHImageManager.default().cancelImageRequest(requestIdValue)
}
}
}
}