Merge pull request #2763 from dreampiggy/fix_sd_imageProgress_auto_create
Change that the `sd_imageProgress` property to not auto-create instance by framework
This commit is contained in:
commit
f3aef5c1a3
|
@ -35,7 +35,7 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
|
|||
* The current image loading progress associated to the view. The unit count is the received size and excepted size of download.
|
||||
* The `totalUnitCount` and `completedUnitCount` will be reset to 0 after a new image loading start (change from current queue). And they will be set to `SDWebImageProgressUnitCountUnknown` if the progressBlock not been called but the image loading success to mark the progress finished (change from main queue).
|
||||
* @note You can use Key-Value Observing on the progress, but you should take care that the change to progress is from a background queue during download(the same as progressBlock). If you want to using KVO and update the UI, make sure to dispatch on the main queue. And it's recommand to use some KVO libs like KVOController because it's more safe and easy to use.
|
||||
* @note The getter will create a progress instance if the value is nil. You can also set a custom progress instance and let it been updated during image loading
|
||||
* @note The getter will create a progress instance if the value is nil. But by default, we don't create one. If you need to use Key-Value Observing, you must trigger the getter or set a custom progresss instance before the loading start. The default value is nil.
|
||||
* @note Note that because of the limitations of categories this property can get out of sync if you update the progress directly.
|
||||
*/
|
||||
@property (nonatomic, strong, null_resettable) NSProgress *sd_imageProgress;
|
||||
|
|
|
@ -69,8 +69,11 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
|||
|
||||
if (url) {
|
||||
// reset the progress
|
||||
self.sd_imageProgress.totalUnitCount = 0;
|
||||
self.sd_imageProgress.completedUnitCount = 0;
|
||||
NSProgress *imageProgress = objc_getAssociatedObject(self, @selector(sd_imageProgress));
|
||||
if (imageProgress) {
|
||||
imageProgress.totalUnitCount = 0;
|
||||
imageProgress.completedUnitCount = 0;
|
||||
}
|
||||
|
||||
#if SD_UIKIT || SD_MAC
|
||||
// check and start image indicator
|
||||
|
@ -83,15 +86,18 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
|||
manager = [SDWebImageManager sharedManager];
|
||||
}
|
||||
|
||||
@weakify(self);
|
||||
SDImageLoaderProgressBlock combinedProgressBlock = ^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
|
||||
@strongify(self);
|
||||
NSProgress *imageProgress = self.sd_imageProgress;
|
||||
imageProgress.totalUnitCount = expectedSize;
|
||||
imageProgress.completedUnitCount = receivedSize;
|
||||
if (imageProgress) {
|
||||
imageProgress.totalUnitCount = expectedSize;
|
||||
imageProgress.completedUnitCount = receivedSize;
|
||||
}
|
||||
#if SD_UIKIT || SD_MAC
|
||||
if ([imageIndicator respondsToSelector:@selector(updateIndicatorProgress:)]) {
|
||||
double progress = imageProgress.fractionCompleted;
|
||||
double progress = 0;
|
||||
if (expectedSize != 0) {
|
||||
progress = (double)receivedSize / expectedSize;
|
||||
}
|
||||
progress = MAX(MIN(progress, 1), 0); // 0.0 - 1.0
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[imageIndicator updateIndicatorProgress:progress];
|
||||
});
|
||||
|
@ -101,13 +107,14 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
|
|||
progressBlock(receivedSize, expectedSize, targetURL);
|
||||
}
|
||||
};
|
||||
@weakify(self);
|
||||
id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options context:context progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
|
||||
@strongify(self);
|
||||
if (!self) { return; }
|
||||
// if the progress not been updated, mark it to complete state
|
||||
if (finished && !error && self.sd_imageProgress.totalUnitCount == 0 && self.sd_imageProgress.completedUnitCount == 0) {
|
||||
self.sd_imageProgress.totalUnitCount = SDWebImageProgressUnitCountUnknown;
|
||||
self.sd_imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown;
|
||||
if (imageProgress && finished && !error && imageProgress.totalUnitCount == 0 && imageProgress.completedUnitCount == 0) {
|
||||
imageProgress.totalUnitCount = SDWebImageProgressUnitCountUnknown;
|
||||
imageProgress.completedUnitCount = SDWebImageProgressUnitCountUnknown;
|
||||
}
|
||||
|
||||
#if SD_UIKIT || SD_MAC
|
||||
|
|
Loading…
Reference in New Issue