@@ -30,10 +30,6 @@ @implementation QMUIAsset {
3030 NSDictionary *_phAssetInfo;
3131 float imageSize;
3232
33- UIImage *_thumbnailImage;
34- UIImage *_originImage;
35- UIImage *_previewImage;
36- PHLivePhoto *_livePhoto;
3733 NSString *_assetIdentityHash;
3834}
3935
@@ -83,10 +79,7 @@ - (instancetype)initWithALAsset:(ALAsset *)alAsset {
8379}
8480
8581- (UIImage *)originImage {
86- if (_originImage) {
87- return _originImage;
88- }
89- __block UIImage *resultImage;
82+ __block UIImage *resultImage = nil ;
9083 if (_usePhotoKit) {
9184 PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc ] init ];
9285 phImageRequestOptions.synchronous = YES ;
@@ -122,14 +115,10 @@ - (UIImage *)originImage {
122115 // 生成最终返回的 UIImage,同时把图片的 orientation 也补充上去
123116 resultImage = [UIImage imageWithCGImage: fullResolutionImageRef scale: [_alAssetRepresentation scale ] orientation: (UIImageOrientation)[_alAssetRepresentation orientation ]];
124117 }
125- _originImage = resultImage;
126118 return resultImage;
127119}
128120
129121- (UIImage *)thumbnailWithSize : (CGSize)size {
130- if (_thumbnailImage) {
131- return _thumbnailImage;
132- }
133122 __block UIImage *resultImage;
134123 if (_usePhotoKit) {
135124 PHImageRequestOptions *phImageRequestOptions = [[PHImageRequestOptions alloc ] init ];
@@ -147,15 +136,11 @@ - (UIImage *)thumbnailWithSize:(CGSize)size {
147136 resultImage = [UIImage imageWithCGImage: thumbnailImageRef];
148137 }
149138 }
150- _thumbnailImage = resultImage;
151139 return resultImage;
152140}
153141
154142- (UIImage *)previewImage {
155- if (_previewImage) {
156- return _previewImage;
157- }
158- __block UIImage *resultImage;
143+ __block UIImage *resultImage = nil ;
159144 if (_usePhotoKit) {
160145 PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
161146 imageRequestOptions.synchronous = YES ;
@@ -170,33 +155,19 @@ - (UIImage *)previewImage {
170155 CGImageRef fullScreenImageRef = [_alAssetRepresentation fullScreenImage ];
171156 resultImage = [UIImage imageWithCGImage: fullScreenImageRef];
172157 }
173- _previewImage = resultImage;
174158 return resultImage;
175159}
176160
177161- (NSInteger )requestOriginImageWithCompletion : (void (^)(UIImage *, NSDictionary *))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
178162 if (_usePhotoKit) {
179- if (_originImage) {
180- // 如果已经有缓存的图片则直接拿缓存的图片
163+ PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
164+ imageRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
165+ imageRequestOptions.progressHandler = phProgressHandler;
166+ return [[[QMUIAssetsManager sharedInstance ] phCachingImageManager ] requestImageForAsset: _phAsset targetSize: PHImageManagerMaximumSize contentMode: PHImageContentModeDefault options: imageRequestOptions resultHandler: ^(UIImage *result, NSDictionary *info) {
181167 if (completion) {
182- completion (_originImage, nil );
168+ completion (result, info );
183169 }
184- return 0 ;
185- } else {
186- PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
187- imageRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
188- imageRequestOptions.progressHandler = phProgressHandler;
189- return [[[QMUIAssetsManager sharedInstance ] phCachingImageManager ] requestImageForAsset: _phAsset targetSize: PHImageManagerMaximumSize contentMode: PHImageContentModeDefault options: imageRequestOptions resultHandler: ^(UIImage *result, NSDictionary *info) {
190- // 排除取消,错误,低清图三种情况,即已经获取到了高清图时,把这张高清图缓存到 _originImage 中
191- BOOL downloadFinined = ![[info objectForKey: PHImageCancelledKey] boolValue ] && ![info objectForKey: PHImageErrorKey] && ![[info objectForKey: PHImageResultIsDegradedKey] boolValue ];
192- if (downloadFinined) {
193- _originImage = result;
194- }
195- if (completion) {
196- completion (result, info);
197- }
198- }];
199- }
170+ }];
200171 } else {
201172 if (completion) {
202173 completion ([self originImage ], nil );
@@ -226,27 +197,14 @@ - (NSInteger)requestThumbnailImageWithSize:(CGSize)size completion:(void (^)(UII
226197
227198- (NSInteger )requestPreviewImageWithCompletion : (void (^)(UIImage *, NSDictionary *))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
228199 if (_usePhotoKit) {
229- if (_previewImage) {
230- // 如果已经有缓存的图片则直接拿缓存的图片
200+ PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
201+ imageRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
202+ imageRequestOptions.progressHandler = phProgressHandler;
203+ return [[[QMUIAssetsManager sharedInstance ] phCachingImageManager ] requestImageForAsset: _phAsset targetSize: CGSizeMake (SCREEN_WIDTH, SCREEN_HEIGHT) contentMode: PHImageContentModeAspectFill options: imageRequestOptions resultHandler: ^(UIImage *result, NSDictionary *info) {
231204 if (completion) {
232- completion (_previewImage, nil );
205+ completion (result, info );
233206 }
234- return 0 ;
235- } else {
236- PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
237- imageRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
238- imageRequestOptions.progressHandler = phProgressHandler;
239- return [[[QMUIAssetsManager sharedInstance ] phCachingImageManager ] requestImageForAsset: _phAsset targetSize: CGSizeMake (SCREEN_WIDTH, SCREEN_HEIGHT) contentMode: PHImageContentModeAspectFill options: imageRequestOptions resultHandler: ^(UIImage *result, NSDictionary *info) {
240- // 排除取消,错误,低清图三种情况,即已经获取到了高清图时,把这张高清图缓存到 _previewImage 中
241- BOOL downloadFinined = ![[info objectForKey: PHImageCancelledKey] boolValue ] && ![info objectForKey: PHImageErrorKey] && ![[info objectForKey: PHImageResultIsDegradedKey] boolValue ];
242- if (downloadFinined) {
243- _previewImage = result;
244- }
245- if (completion) {
246- completion (result, info);
247- }
248- }];
249- }
207+ }];
250208 } else {
251209 if (completion) {
252210 completion ([self previewImage ], nil );
@@ -257,27 +215,14 @@ - (NSInteger)requestPreviewImageWithCompletion:(void (^)(UIImage *, NSDictionary
257215
258216- (NSInteger )requestLivePhotoWithCompletion : (void (^)(PHLivePhoto *, NSDictionary *))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
259217 if (_usePhotoKit && [[PHCachingImageManager class ] instancesRespondToSelector: @selector (requestLivePhotoForAsset:targetSize:contentMode:options:resultHandler: )]) {
260- if (_livePhoto) {
261- // 如果已经有缓存的 LivePhoto 则直接拿缓存的 LivePhoto
218+ PHLivePhotoRequestOptions *livePhotoRequestOptions = [[PHLivePhotoRequestOptions alloc ] init ];
219+ livePhotoRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
220+ livePhotoRequestOptions.progressHandler = phProgressHandler;
221+ return [[[QMUIAssetsManager sharedInstance ] phCachingImageManager ] requestLivePhotoForAsset: _phAsset targetSize: CGSizeMake (SCREEN_WIDTH, SCREEN_HEIGHT) contentMode: PHImageContentModeDefault options: livePhotoRequestOptions resultHandler: ^(PHLivePhoto * _Nullable livePhoto, NSDictionary * _Nullable info) {
262222 if (completion) {
263- completion (_livePhoto, nil );
223+ completion (livePhoto, info );
264224 }
265- return 0 ;
266- } else {
267- PHLivePhotoRequestOptions *livePhotoRequestOptions = [[PHLivePhotoRequestOptions alloc ] init ];
268- livePhotoRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
269- livePhotoRequestOptions.progressHandler = phProgressHandler;
270- return [[[QMUIAssetsManager sharedInstance ] phCachingImageManager ] requestLivePhotoForAsset: _phAsset targetSize: CGSizeMake (SCREEN_WIDTH, SCREEN_HEIGHT) contentMode: PHImageContentModeDefault options: livePhotoRequestOptions resultHandler: ^(PHLivePhoto * _Nullable livePhoto, NSDictionary * _Nullable info) {
271- // 排除取消,错误,低清图三种情况,即已经获取到了高清图时,把这张高清图缓存到 _livePhoto
272- BOOL downloadFinined = ![[info objectForKey: PHLivePhotoInfoCancelledKey] boolValue ] && ![info objectForKey: PHLivePhotoInfoErrorKey] && ![[info objectForKey: PHLivePhotoInfoIsDegradedKey] boolValue ];
273- if (downloadFinined) {
274- _livePhoto = livePhoto;
275- }
276- if (completion) {
277- completion (livePhoto, info);
278- }
279- }];
280- }
225+ }];
281226 } else {
282227 return 0 ;
283228 }
0 commit comments