-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMWPhotoYYWebImage.m
83 lines (68 loc) · 2.62 KB
/
MWPhotoYYWebImage.m
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
//
// MWPhotoYYWebImage.m
//
// Created by LinXi on 6/1/16.
// Copyright © 2016 All rights reserved.
//
#import "MWPhotoYYWebImage.h"
#import "YYWebImageManager.h"
@interface MWPhoto ()
- (void)imageLoadingComplete;
- (void)cancelImageRequest;
- (void)cancelVideoRequest;
@end
@interface MWPhotoYYWebImage ()
@property(nonatomic, strong) YYWebImageOperation *imageDownloadOperation;
@end
@implementation MWPhotoYYWebImage
+ (MWPhoto *)photoWithImage:(UIImage *)image {
return [[MWPhotoYYWebImage alloc] initWithImage:image];
}
+ (MWPhoto *)photoWithURL:(NSURL *)url {
return [[MWPhotoYYWebImage alloc] initWithURL:url];
}
+ (MWPhoto *)photoWithAsset:(PHAsset *)asset targetSize:(CGSize)targetSize {
return [[MWPhotoYYWebImage alloc] initWithAsset:asset targetSize:targetSize];
}
+ (MWPhoto *)videoWithURL:(NSURL *)url {
return [[MWPhotoYYWebImage alloc] initWithVideoURL:url];
}
// Load from local file
- (void)_performLoadUnderlyingImageAndNotifyWithWebURL:(NSURL *)url {
@try {
YYWebImageManager *manager = [YYWebImageManager sharedManager];
_imageDownloadOperation = [manager requestImageWithURL:url options:YYWebImageOptionShowNetworkActivity progress:^(NSInteger receivedSize, NSInteger expectedSize) {
if (expectedSize > 0) {
float progress = receivedSize / (float)expectedSize;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:progress], @"progress",
self, @"photo", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:MWPHOTO_PROGRESS_NOTIFICATION object:dict];
}
}
transform:nil
completion:^(UIImage *_Nullable image, NSURL *_Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError *_Nullable error) {
if (error) {
MWLog(@"SDWebImage failed to download image: %@", error);
}
_imageDownloadOperation = nil;
self.underlyingImage = image;
dispatch_async(dispatch_get_main_queue(), ^{
[self imageLoadingComplete];
});
}];
} @catch (NSException *e) {
MWLog(@"Photo from web: %@", e);
_imageDownloadOperation = nil;
[self imageLoadingComplete];
}
}
- (void)cancelAnyLoading {
if (_imageDownloadOperation != nil) {
[_imageDownloadOperation cancel];
[self setValue:@(NO) forKey:@"_loadingInProgress"];
}
[self cancelImageRequest];
[self cancelVideoRequest];
}
@end