1414#import " QMUIAssetsManager.h"
1515#import " NSString+QMUI.h"
1616
17+ static NSString * const kAssetInfoImageData = @" imageData" ;
18+ static NSString * const kAssetInfoOriginInfo = @" originInfo" ;
19+ static NSString * const kAssetInfoDataUTI = @" dataUTI" ;
20+ static NSString * const kAssetInfoOrientation = @" orientation" ;
21+ static NSString * const kAssetInfoSize = @" size" ;
22+
1723@interface QMUIAsset ()
1824
1925@property (nonatomic , assign , readwrite ) QMUIAssetType assetType;
@@ -159,7 +165,7 @@ - (UIImage *)previewImage {
159165 return resultImage;
160166}
161167
162- - (NSInteger )requestOriginImageWithCompletion : (void (^)(UIImage *result, NSDictionary *info))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
168+ - (NSInteger )requestOriginImageWithCompletion : (void (^)(UIImage *result, NSDictionary <NSString *, id> *info))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
163169 if (_usePhotoKit) {
164170 PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
165171 imageRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
@@ -177,7 +183,7 @@ - (NSInteger)requestOriginImageWithCompletion:(void (^)(UIImage *result, NSDicti
177183 }
178184}
179185
180- - (NSInteger )requestThumbnailImageWithSize : (CGSize)size completion : (void (^)(UIImage *result, NSDictionary *info))completion {
186+ - (NSInteger )requestThumbnailImageWithSize : (CGSize)size completion : (void (^)(UIImage *result, NSDictionary <NSString *, id> *info))completion {
181187 if (_usePhotoKit) {
182188 PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
183189 imageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
@@ -196,7 +202,7 @@ - (NSInteger)requestThumbnailImageWithSize:(CGSize)size completion:(void (^)(UII
196202 }
197203}
198204
199- - (NSInteger )requestPreviewImageWithCompletion : (void (^)(UIImage *result, NSDictionary *info))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
205+ - (NSInteger )requestPreviewImageWithCompletion : (void (^)(UIImage *result, NSDictionary <NSString *, id> *info))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
200206 if (_usePhotoKit) {
201207 PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc ] init ];
202208 imageRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
@@ -214,7 +220,7 @@ - (NSInteger)requestPreviewImageWithCompletion:(void (^)(UIImage *result, NSDict
214220 }
215221}
216222
217- - (NSInteger )requestLivePhotoWithCompletion : (void (^)(PHLivePhoto *livePhoto, NSDictionary *info))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
223+ - (NSInteger )requestLivePhotoWithCompletion : (void (^)(PHLivePhoto *livePhoto, NSDictionary <NSString *, id> *info))completion withProgressHandler : (PHAssetImageProgressHandler)phProgressHandler {
218224 if (_usePhotoKit && [[PHCachingImageManager class ] instancesRespondToSelector: @selector (requestLivePhotoForAsset:targetSize:contentMode:options:resultHandler: )]) {
219225 PHLivePhotoRequestOptions *livePhotoRequestOptions = [[PHLivePhotoRequestOptions alloc ] init ];
220226 livePhotoRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
@@ -229,7 +235,7 @@ - (NSInteger)requestLivePhotoWithCompletion:(void (^)(PHLivePhoto *livePhoto, NS
229235 }
230236}
231237
232- - (NSInteger )requestPlayerItemWithCompletion : (void (^)(AVPlayerItem *playerItem, NSDictionary *info))completion withProgressHandler : (PHAssetVideoProgressHandler)phProgressHandler {
238+ - (NSInteger )requestPlayerItemWithCompletion : (void (^)(AVPlayerItem *playerItem, NSDictionary <NSString *, id> *info))completion withProgressHandler : (PHAssetVideoProgressHandler)phProgressHandler {
233239 if (_usePhotoKit && [[PHCachingImageManager class ] instancesRespondToSelector: @selector (requestPlayerItemForVideo:options:resultHandler: )]) {
234240 PHVideoRequestOptions *videoRequestOptions = [[PHVideoRequestOptions alloc ] init ];
235241 videoRequestOptions.networkAccessAllowed = YES ; // 允许访问网络
@@ -249,10 +255,10 @@ - (NSInteger)requestPlayerItemWithCompletion:(void (^)(AVPlayerItem *playerItem,
249255 }
250256}
251257
252- - (void )requestImageData : (void (^)(NSData *imageData, BOOL isGif))completion {
258+ - (void )requestImageData : (void (^)(NSData *imageData, NSDictionary <NSString *, id> *info, BOOL isGif))completion {
253259 if (self.assetType != QMUIAssetTypeImage && self.assetType != QMUIAssetTypeLivePhoto) {
254260 if (completion) {
255- completion (nil , NO );
261+ completion (nil , nil , NO );
256262 }
257263 return ;
258264 }
@@ -262,22 +268,24 @@ - (void)requestImageData:(void (^)(NSData *imageData, BOOL isGif))completion {
262268 [self requestPhAssetInfo: ^(NSDictionary *phAssetInfo) {
263269 _phAssetInfo = phAssetInfo;
264270 if (completion) {
265- NSString *dataUTI = phAssetInfo[@" dataUTI " ];
271+ NSString *dataUTI = phAssetInfo[kAssetInfoDataUTI ];
266272 BOOL isGif = [dataUTI isEqualToString: (__bridge NSString *)kUTTypeGIF ];
273+ NSDictionary <NSString *, id > *originInfo = phAssetInfo[kAssetInfoOriginInfo ];
267274 /* *
268275 * 这里不在主线程执行,若用户在该 block 中操作 UI 时会产生一些问题,
269276 * 为了避免这种情况,这里该 block 主动放到主线程执行。
270277 */
271278 dispatch_async (dispatch_get_main_queue (), ^{
272- completion (phAssetInfo[@" imageData " ] , isGif);
279+ completion (phAssetInfo[kAssetInfoImageData ], originInfo , isGif);
273280 });
274281 }
275282 }];
276283 } else {
277284 if (completion) {
278- NSString *dataUTI = _phAssetInfo[@" dataUTI " ];
285+ NSString *dataUTI = _phAssetInfo[kAssetInfoDataUTI ];
279286 BOOL isGif = [dataUTI isEqualToString: (__bridge NSString *)kUTTypeGIF ];
280- completion (_phAssetInfo[@" imageData" ], isGif);
287+ NSDictionary <NSString *, id > *originInfo = _phAssetInfo[kAssetInfoOriginInfo ];
288+ completion (_phAssetInfo[kAssetInfoImageData ], originInfo, isGif);
281289 }
282290 }
283291 } else {
@@ -292,9 +300,9 @@ - (void)requestImageData:(void (^)(NSData *imageData, BOOL isGif))completion {
292300 // 判断是否为 GIF 图
293301 ALAssetRepresentation *gifRepresentation = [_alAsset representationForUTI: (__bridge NSString *)kUTTypeGIF ];
294302 if (gifRepresentation) {
295- completion (imageData, YES );
303+ completion (imageData, nil , YES );
296304 } else {
297- completion (imageData, NO );
305+ completion (imageData, nil , NO );
298306 }
299307 }];
300308 }
@@ -312,7 +320,7 @@ - (UIImageOrientation)imageOrientation {
312320 } synchronous: YES ];
313321 }
314322 // 从 PhAssetInfo 中获取 UIImageOrientation 对应的字段
315- orientation = (UIImageOrientation)[_phAssetInfo[@" orientation " ] integerValue ];
323+ orientation = (UIImageOrientation)[_phAssetInfo[kAssetInfoOrientation ] integerValue ];
316324 } else {
317325 orientation = (UIImageOrientation)[[_alAsset valueForProperty: @" ALAssetPropertyOrientation" ] integerValue ];
318326 }
@@ -347,13 +355,13 @@ - (void)requestPhAssetInfo:(void (^)(NSDictionary *))completion {
347355 if ([asset isKindOfClass: [AVURLAsset class ]]) {
348356 NSMutableDictionary *tempInfo = [[NSMutableDictionary alloc ] init ];
349357 if (info) {
350- [tempInfo addEntriesFromDictionary : info];
358+ [tempInfo setObject : info forKey: kAssetInfoOriginInfo ];
351359 }
352360
353361 AVURLAsset *urlAsset = (AVURLAsset*)asset;
354362 NSNumber *size;
355363 [urlAsset.URL getResourceValue: &size forKey: NSURLFileSizeKey error: nil ];
356- [tempInfo setObject: size forKey: @" size " ];
364+ [tempInfo setObject: size forKey: kAssetInfoSize ];
357365 if (completion) {
358366 completion (tempInfo);
359367 }
@@ -375,14 +383,14 @@ - (void)requestImagePhAssetInfo:(void (^)(NSDictionary *))completion synchronous
375383 [[[QMUIAssetsManager sharedInstance ] phCachingImageManager ] requestImageDataForAsset: _phAsset options: imageRequestOptions resultHandler: ^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
376384 if (info) {
377385 NSMutableDictionary *tempInfo = [[NSMutableDictionary alloc ] init ];
378- [tempInfo setObject: imageData forKey: @" imageData " ];
386+ [tempInfo setObject: imageData forKey: kAssetInfoImageData ];
379387
380- [tempInfo addEntriesFromDictionary : info];
388+ [tempInfo setObject : info forKey: kAssetInfoOriginInfo ];
381389 if (dataUTI) {
382- [tempInfo setObject: dataUTI forKey: @" dataUTI " ]; // TODO: kayo 这个字段对应的是不是就是文件的 data?换句话说,要支持 GIF 就要从这里获取 data?
390+ [tempInfo setObject: dataUTI forKey: kAssetInfoDataUTI ];
383391 }
384- [tempInfo setObject: @(orientation) forKey: @" orientation " ];
385- [tempInfo setObject: @(imageData.length) forKey: @" size " ];
392+ [tempInfo setObject: @(orientation) forKey: kAssetInfoOrientation ];
393+ [tempInfo setObject: @(imageData.length) forKey: kAssetInfoSize ];
386394 if (completion) {
387395 completion (tempInfo);
388396 }
@@ -411,13 +419,13 @@ - (void)assetSize:(void (^)(long long size))completion {
411419 * 为了避免这种情况,这里该 block 主动放到主线程执行。
412420 */
413421 dispatch_async (dispatch_get_main_queue (), ^{
414- completion ([phAssetInfo[@" size " ] longLongValue ]);
422+ completion ([phAssetInfo[kAssetInfoSize ] longLongValue ]);
415423 });
416424 }
417425 }];
418426 } else {
419427 if (completion) {
420- completion ([_phAssetInfo[@" size " ] longLongValue ]);
428+ completion ([_phAssetInfo[kAssetInfoSize ] longLongValue ]);
421429 }
422430 }
423431 } else {
0 commit comments