Skip to content

Commit

Permalink
SDWebImage缓存key标准化;动效出场偶发闪烁BUG修复;代码优化;
Browse files Browse the repository at this point in the history
  • Loading branch information
shenguanjiejie authored and indulgeIn committed Feb 27, 2019
1 parent 2d2fd6b commit 899079c
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 120 deletions.
2 changes: 1 addition & 1 deletion YBImageBrowser.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pod::Spec.new do |s|


s.name = "YBImageBrowser"
s.version = "2.1.3"
s.version = "2.1.4"
s.summary = "image browser for iOS (powerful, superior performance)"
s.description = <<-DESC
image browser for iOS (powerful, superior performance),
Expand Down
3 changes: 2 additions & 1 deletion YBImageBrowser/Base/YBImageBrowserView.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "YBIBLayoutDirectionManager.h"
#import "YBIBUtilities.h"
#import "YBIBGestureInteractionProfile.h"
#import "YBImageBrowser.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -32,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, weak) id<YBImageBrowserDataSource> yb_dataSource;

@property (nonatomic, weak) UIViewController<YBImageBrowserViewDelegate> *yb_delegate;
@property (nonatomic, weak) YBImageBrowser<YBImageBrowserViewDelegate> *yb_delegate;

@property (nonatomic, assign, readonly) NSUInteger currentIndex;

Expand Down
2 changes: 1 addition & 1 deletion YBImageBrowser/Helper/YBIBTransitionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, weak) YBImageBrowser *imageBrowser;

@property (nonatomic, assign, readonly) BOOL isTransitioning;
@property (nonatomic, assign, readonly) BOOL transitioning;

@end

Expand Down
20 changes: 10 additions & 10 deletions YBImageBrowser/Helper/YBIBTransitionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#import "YBImageBrowserCellProtocol.h"

@interface YBIBTransitionManager () {
BOOL _isEnter;
BOOL _enter;
}
@property (nonatomic, strong) UIImageView *animateImageView;
@property (nonatomic, assign) BOOL isTransitioning;
@property (nonatomic, assign) BOOL transitioning;
@end

@implementation YBIBTransitionManager
Expand All @@ -25,8 +25,8 @@ @implementation YBIBTransitionManager
- (instancetype)init {
self = [super init];
if (self) {
self.isTransitioning = NO;
self->_isEnter = NO;
self.transitioning = NO;
self->_enter = NO;
}
return self;
}
Expand All @@ -35,7 +35,7 @@ - (instancetype)init {

- (void)animationEnded:(BOOL)transitionCompleted {
if (self.imageBrowser && self.imageBrowser.delegate && [self.imageBrowser.delegate respondsToSelector:@selector(yb_imageBrowser:transitionAnimationEndedWithIsEnter:)]) {
[self.imageBrowser.delegate yb_imageBrowser:self.imageBrowser transitionAnimationEndedWithIsEnter:self->_isEnter];
[self.imageBrowser.delegate yb_imageBrowser:self.imageBrowser transitionAnimationEndedWithIsEnter:self->_enter];
}
}

Expand All @@ -52,8 +52,8 @@ - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionC

// Enter
if (toController.isBeingPresented) {
self->_isEnter = YES;
self.isTransitioning = YES;
self->_enter = YES;
self.transitioning = YES;
switch (self.imageBrowser.enterTransitionType) {
case YBImageBrowserTransitionTypeNone: {
[containerView addSubview:toView];
Expand Down Expand Up @@ -99,8 +99,8 @@ - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionC

// Out
if (fromController.isBeingDismissed) {
self->_isEnter = NO;
self.isTransitioning = YES;
self->_enter = NO;
self.transitioning = YES;
switch (self.imageBrowser.outTransitionType) {
case YBImageBrowserTransitionTypeNone: {
[self completeTransition:transitionContext isEnter:NO];
Expand Down Expand Up @@ -149,7 +149,7 @@ - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionC

- (void)completeTransition:(nullable id <UIViewControllerContextTransitioning>)transitionContext isEnter:(BOOL)isEnter {
[transitionContext completeTransition:!transitionContext.transitionWasCancelled];
self.isTransitioning = NO;
self.transitioning = NO;
if (!isEnter) {
self.imageBrowser.hiddenSourceObject = nil;
}
Expand Down
26 changes: 15 additions & 11 deletions YBImageBrowser/Helper/YBIBWebImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
//

#import "YBIBWebImageManager.h"
#if __has_include(<SDWebImage/SDWebImageDownloader.h>)
#import <SDWebImage/SDWebImageDownloader.h>
#if __has_include(<SDWebImage/SDWebImageManager.h>)
#import <SDWebImage/SDWebImageManager.h>
#else
#import "SDWebImageDownloader.h"
#endif
#if __has_include(<SDWebImage/SDImageCache.h>)
#import <SDWebImage/SDImageCache.h>
#else
#import "SDImageCache.h"
#import "SDWebImageManager.h"
#endif

static BOOL _downloaderShouldDecompressImages;
Expand Down Expand Up @@ -62,13 +57,22 @@ + (void)cancelTaskWithDownloadToken:(id)token {
}

+ (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSURL *)key toDisk:(BOOL)toDisk {
[[SDImageCache sharedImageCache] storeImage:image imageData:data forKey:key.absoluteString toDisk:toDisk completion:nil];
if (!key) return;
NSString *cacheKey = [SDWebImageManager.sharedManager cacheKeyForURL:key];
if (!cacheKey) return;

[[SDImageCache sharedImageCache] storeImage:image imageData:data forKey:cacheKey toDisk:toDisk completion:nil];
}

+ (void)queryCacheOperationForKey:(NSURL *)key completed:(YBIBWebImageManagerCacheQueryCompletedBlock)completed {
if (!key) return;
#define QUERY_CACHE_FAILED if (completed) {completed(nil, nil); return;}
if (!key) QUERY_CACHE_FAILED
NSString *cacheKey = [SDWebImageManager.sharedManager cacheKeyForURL:key];
if (!cacheKey) QUERY_CACHE_FAILED
#undef QUERY_CACHE_FAILED

SDImageCacheOptions options = SDImageCacheQueryDataWhenInMemory;
[[SDImageCache sharedImageCache] queryCacheOperationForKey:key.absoluteString options:options done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
[[SDImageCache sharedImageCache] queryCacheOperationForKey:cacheKey options:options done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
if (completed) {
completed(image, data);
}
Expand Down
68 changes: 41 additions & 27 deletions YBImageBrowser/ImageBrowse/YBImageBrowseCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
@interface YBImageBrowseCell () <YBImageBrowserCellProtocol, UIScrollViewDelegate, UIGestureRecognizerDelegate> {
YBImageBrowserLayoutDirection _layoutDirection;
CGSize _containerSize;
BOOL _isZooming;
BOOL _isDragging;
BOOL _bodyIsInCenter;
BOOL _zooming;
BOOL _dragging;
BOOL _bodyInCenter;
BOOL _outTransitioning;

CGPoint _gestureInteractionStartPoint;
BOOL _isGestureInteraction;
BOOL _gestureInteracting;
YBIBGestureInteractionProfile *_giProfile;

UIInterfaceOrientation _statusBarOrientationBefore;
Expand Down Expand Up @@ -63,6 +64,11 @@ - (instancetype)initWithFrame:(CGRect)frame {
return self;
}

- (void)willMoveToWindow:(UIWindow *)newWindow {
[super willMoveToWindow:newWindow];
self->_outTransitioning = NO;
}

- (void)prepareForReuse {
[self initVars];
[self removeObserverForDataState];
Expand All @@ -76,14 +82,15 @@ - (void)prepareForReuse {
}

- (void)initVars {
self->_isZooming = NO;
self->_isDragging = NO;
self->_bodyIsInCenter = YES;
self->_zooming = NO;
self->_dragging = NO;
self->_bodyInCenter = YES;
self->_outTransitioning = NO;
self->_layoutDirection = YBImageBrowserLayoutDirectionUnknown;
self->_containerSize = CGSizeMake(1, 1);

self->_gestureInteractionStartPoint = CGPointZero;
self->_isGestureInteraction = NO;
self->_gestureInteracting = NO;
}

#pragma mark - <YBImageBrowserCellProtocol>
Expand All @@ -106,7 +113,7 @@ - (void)yb_browserLayoutDirectionChanged:(YBImageBrowserLayoutDirection)layoutDi

[self hideTailoringImageView];

if (self->_isGestureInteraction) {
if (self->_gestureInteracting) {
[self restoreGestureInteractionWithDuration:0];
}

Expand All @@ -115,11 +122,10 @@ - (void)yb_browserLayoutDirectionChanged:(YBImageBrowserLayoutDirection)layoutDi
}

- (void)yb_browserBodyIsInTheCenter:(BOOL)isIn {
self->_bodyIsInCenter = isIn;
self->_bodyInCenter = isIn;
}

- (UIView *)yb_browserCurrentForegroundView {
[self hideTailoringImageView];
return self.mainImageView;
}

Expand Down Expand Up @@ -163,21 +169,21 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
self->_isZooming = YES;
self->_zooming = YES;
[self hideTailoringImageView];
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale {
self->_isZooming = NO;
self->_zooming = NO;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
self->_isDragging = YES;
self->_dragging = YES;
[self hideTailoringImageView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
self->_isDragging = NO;
self->_dragging = NO;
}

#pragma mark - <UIGestureRecognizerDelegate>
Expand All @@ -203,7 +209,7 @@ - (void)addGesture {
}

- (void)respondsToTapSingle:(UITapGestureRecognizer *)tap {
self.yb_browserDismissBlock();
[self browserDismiss];
}

- (void)respondsToTapDouble:(UITapGestureRecognizer *)tap {
Expand Down Expand Up @@ -239,15 +245,15 @@ - (void)respondsToPan:(UIPanGestureRecognizer *)pan {
} else if (pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateRecognized || pan.state == UIGestureRecognizerStateFailed) {

// END
if (self->_isGestureInteraction) {
if (self->_gestureInteracting) {
CGPoint velocity = [pan velocityInView:self.mainContentView];

BOOL velocityArrive = ABS(velocity.y) > self->_giProfile.dismissVelocityY;
BOOL distanceArrive = ABS(point.y - self->_gestureInteractionStartPoint.y) > self->_containerSize.height * self->_giProfile.dismissScale;

BOOL shouldDismiss = distanceArrive || velocityArrive;
if (shouldDismiss) {
self.yb_browserDismissBlock();
[self browserDismiss];
} else {
[self restoreGestureInteractionWithDuration:self->_giProfile.restoreDuration];
}
Expand All @@ -263,11 +269,11 @@ - (void)respondsToPan:(UIPanGestureRecognizer *)pan {
BOOL upArrive = point.y - self->_gestureInteractionStartPoint.y > triggerDistance && self.mainContentView.contentOffset.y <= 1,
downArrive = point.y - self->_gestureInteractionStartPoint.y < -triggerDistance && self.mainContentView.contentOffset.y + self.mainContentView.bounds.size.height >= MAX(self.mainContentView.contentSize.height, self.mainContentView.bounds.size.height) - 1;

BOOL shouldStart = startPointValid && !self->_isGestureInteraction && (upArrive || downArrive) && distanceArrive && self->_bodyIsInCenter && !self->_isZooming;
BOOL shouldStart = startPointValid && !self->_gestureInteracting && (upArrive || downArrive) && distanceArrive && self->_bodyInCenter && !self->_zooming;
// START
if (shouldStart) {
if ([UIApplication sharedApplication].statusBarOrientation != self->_statusBarOrientationBefore) {
self.yb_browserDismissBlock();
[self browserDismiss];
} else {
[self hideTailoringImageView];

Expand All @@ -283,12 +289,12 @@ - (void)respondsToPan:(UIPanGestureRecognizer *)pan {
self.yb_browserScrollEnabledBlock(NO);
self.yb_browserToolBarHiddenBlock(YES);

self->_isGestureInteraction = YES;
self->_gestureInteracting = YES;
}
}

// CHNAGE
if (self->_isGestureInteraction) {
if (self->_gestureInteracting) {
self.mainContentView.center = point;
CGFloat scale = 1 - ABS(point.y - self->_gestureInteractionStartPoint.y) / (self->_containerSize.height * 1.2);
if (scale > 1) scale = 1;
Expand Down Expand Up @@ -319,7 +325,7 @@ - (void)restoreGestureInteractionWithDuration:(NSTimeInterval)duration {
self.mainContentView.scrollEnabled = YES;

self->_gestureInteractionStartPoint = CGPointZero;
self->_isGestureInteraction = NO;
self->_gestureInteracting = NO;

[self cutImage];
};
Expand All @@ -343,13 +349,21 @@ - (void)removeObserverForDataState {
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if (object == self.cellData && [keyPath isEqualToString:@"dataState"]) {
if (!self->_outTransitioning && object == self.cellData && [keyPath isEqualToString:@"dataState"]) {
[self cellDataStateChanged];
}
}

#pragma mark - private

- (void)browserDismiss {
self->_outTransitioning = YES;
[self hideTailoringImageView];
[self.contentView yb_hideProgressView];
[self yb_hideProgressView];
self.yb_browserDismissBlock();
}

- (void)cellDataStateChanged {
YBImageBrowseCellData *data = self.cellData;
YBImageBrowseCellDataState dataState = data.dataState;
Expand Down Expand Up @@ -459,7 +473,7 @@ - (void)updateMainContentViewLayoutWithContainerSize:(CGSize)containerSize fillT
}

- (void)showTailoringImageView:(UIImage *)image {
if (self->_isGestureInteraction) return;
if (self->_gestureInteracting) return;
if (!self.tailoringImageView.superview) {
[self.contentView addSubview:self.tailoringImageView];
}
Expand All @@ -476,7 +490,7 @@ - (void)hideTailoringImageView {
}

- (void)cutImage {
if ([self.cellData needCompress] && !self.cellData.isCutting && self.mainContentView.zoomScale > 1.15) {
if ([self.cellData needCompress] && !self.cellData.cutting && self.mainContentView.zoomScale > 1.15) {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_cutImage) object:nil];
[self performSelector:@selector(_cutImage) withObject:nil afterDelay:0.25];
}
Expand All @@ -491,7 +505,7 @@ - (void)_cutImage {

YBImageBrowseCellData *tmp = self.cellData;
[self.cellData cuttingImageToRect:CGRectMake(x, y, width, height) complete:^(UIImage *image) {
if (tmp == self.cellData && !self->_isDragging) {
if (tmp == self.cellData && !self->_dragging) {
[self showTailoringImageView:image];
}
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ typedef NS_ENUM(NSInteger, YBImageBrowseCellDataState) {

@property (nonatomic, strong) UIImage *compressImage;
@property (nonatomic, assign) CGFloat downloadProgress;
@property (nonatomic, assign) BOOL isCutting;
@property (nonatomic, assign) BOOL cutting;
@property (nonatomic, assign) CGFloat zoomScale;

- (void)loadData;
Expand Down
Loading

0 comments on commit 899079c

Please sign in to comment.