Skip to content

Commit

Permalink
优化部分功能
Browse files Browse the repository at this point in the history
对预加载的图片的方式进行了优化和完善
  • Loading branch information
tianliangyihou committed Jan 10, 2018
1 parent 77e0ce9 commit f0f8bac
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
3 changes: 3 additions & 0 deletions test.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@
baseConfigurationReference = 52839D449824474097575972 /* Pods-test.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
Expand All @@ -560,6 +561,7 @@
baseConfigurationReference = 7258DD35A57A1219D711F5E6 /* Pods-test.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
Expand All @@ -568,6 +570,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.vcyber.test;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
};
name = Release;
Expand Down
1 change: 1 addition & 0 deletions test/Class/style3/LBStyle3VC.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
[[LBPhotoBrowserManager defaultManager] addPlaceholdImageCallBackBlock:^UIImage *(NSIndexPath *indexPath) {
return [UIImage imageNamed:@"placehold.jpeg"];
}];

}];
return cell;
}
Expand Down
1 change: 0 additions & 1 deletion test/LBPhotoBrowser/LBPhotoBrowserManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
// 每张正在加载的图片的站位图的大小
@property (nonatomic , copy ,readonly)CGSize (^placeholdImageSizeBlock)(UIImage *Image,NSIndexPath *indexpath);


/**
开启这个选项后 在加载gif的时候 会大大的降低内存.与YYImage对gif的内存优化思路一样 default is NO
每次LBPhotoBrowser -> did dismiss(消失)的时候,LBPhotoBrowserManager 会将 lowGifMemory 置为NO,
Expand Down
1 change: 0 additions & 1 deletion test/LBPhotoBrowser/LBPhotoBrowserView.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

@property (nonatomic , assign)BOOL isShowing;
@property (nonatomic , assign)BOOL showPopAnimation;
@property (nonatomic , assign)BOOL shouldCancel;
@property (nonatomic , assign)int index;

@property (nonatomic , copy)void (^loadImageCompletedBlock)(LBScrollViewStatusModel *loadModel,UIImage *image, NSData *data, NSError * error, BOOL finished, NSURL *imageURL);
Expand Down
81 changes: 47 additions & 34 deletions test/LBPhotoBrowser/LBPhotoBrowserView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ @interface LBPhotoBrowserView ()<UICollectionViewDelegate,UICollectionViewDataSo

@property (nonatomic , assign)CGFloat zoomScale;

@property (nonatomic , assign)BOOL stopPreloading;

@property (nonatomic , assign)CGPoint startCenter;

@property (nonatomic , strong)NSMutableDictionary *loadingImageModelDic;
@property (nonatomic , strong)NSMutableDictionary *preloadingModelDic;
//GCD中的对象在6.0之前是不参与ARC的,而6.0之后 在ARC下使用GCD也不用关心释放问题
@property (strong, nonatomic) dispatch_queue_t preloadingQueue;

@end

Expand All @@ -79,15 +81,16 @@ - (void)loadImage {
if (self.opreation) {
return;
}
//Code=-999 "已取消"
self.opreation = [[SDWebImageManager sharedManager] loadImageWithURL:self.url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {

//LBPhotoBrowserLog(@"LBScrollViewStatusModel:( %d ) line %d log: %d -- %d",self.index,__LINE__,(int)receivedSize ,(int)expectedSize);
// LBPhotoBrowserLog(@"LBScrollViewStatusModel:( %d ) line %d log: %d -- %d",self.index,__LINE__,(int)receivedSize ,(int)expectedSize);
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
__block UIImage *downloadedImage = image;
dispatch_async(dispatch_get_main_queue(), ^{
wself.opreation = nil;
if (wself.loadImageCompletedBlock) {
wself.loadImageCompletedBlock(self, downloadedImage, data, error, finished, imageURL);
wself.loadImageCompletedBlock(wself, downloadedImage, data, error, finished, imageURL);
}else {
if (error) {
downloadedImage = [LBPhotoBrowserManager defaultManager].errorImage;
Expand Down Expand Up @@ -154,6 +157,7 @@ @implementation LBPhotoBrowserView

- (void)dealloc {
[[NSNotificationCenter defaultCenter]removeObserver:self];
SDDispatchQueueRelease(_preloadingQueue);
}

- (NSMutableArray *)models {
Expand All @@ -162,6 +166,19 @@ - (NSMutableArray *)models {
}
return _models;
}
- (NSMutableDictionary *)preloadingModelDic {
if (!_preloadingModelDic) {
_preloadingModelDic = [[NSMutableDictionary alloc]init];
}
return _preloadingModelDic;
}

- (NSMutableDictionary *)loadingImageModelDic {
if (!_loadingImageModelDic) {
_loadingImageModelDic = [[NSMutableDictionary alloc]init];
}
return _loadingImageModelDic;
}

- (UIPageControl *)pageControl {
if (!_pageControl) {
Expand Down Expand Up @@ -215,8 +232,8 @@ - (instancetype)initWithFrame:(CGRect)frame
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
[self addGestureRecognizer:pan];
[LBPhotoBrowserManager defaultManager].currentCollectionView = self.collectionView;
_preloadingQueue = dispatch_queue_create("lb.photoBrowser", DISPATCH_QUEUE_SERIAL);
_isShowing = NO;
_stopPreloading = NO;
}
return self;
}
Expand Down Expand Up @@ -288,6 +305,7 @@ - (void)cancelFromCell:(LBPhotoCollectionViewCell *)cell {
}

#pragma mark - 监听通知

- (void)removePageControl {
[UIView animateWithDuration:0.25 animations:^{
self.pageControl.alpha = 0;
Expand All @@ -297,7 +315,7 @@ - (void)removePageControl {
if (![LBPhotoBrowserManager defaultManager].needPreloading) {
return;
}
[self.models enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.loadingImageModelDic.allValues enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
LBScrollViewStatusModel *model = (LBScrollViewStatusModel *)obj;
if (model.opreation) {
[model.opreation cancel];
Expand Down Expand Up @@ -366,7 +384,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
}
#pragma mark - 代理方法

// 新版的SDWebImage不知支持Gif 故采用了老版Gif的方式 但是这样加载太多Gif内存容易升高 在收到内存警告的时候 可以通过这个来清理内存 [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"]
// 新版的SDWebImage不知支持Gif 故采用了老版Gif的方式 但是这样加载太多Gif内存容易升高 在收到内存警告的时候 可以通过这个来清理内存 [[SDImageCache sharedImageCache] clearMemory];

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(SCREEN_WIDTH + itemSpace, SCREEN_HEIGHT);
Expand Down Expand Up @@ -395,42 +413,37 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICol

if (![LBPhotoBrowserManager defaultManager].needPreloading) return;

if (self.stopPreloading) return;

// 异步线程加载数据
dispatch_async(dispatch_get_global_queue(0, 0), ^{

for (LBScrollViewStatusModel *obj in wself.models) {
obj.shouldCancel = YES;
}

dispatch_async(wself.preloadingQueue, ^{
int leftCellIndex = model.index - 1 >= 0 ?model.index - 1:0;
int rightCellIndex = model.index + 1 < wself.models.count? model.index + 1 : (int)wself.models.count -1;

//wself.loadingImageModels 新计算出的需要加载的 -- > 如果个原来的没有重合的 --> 取消
[wself.preloadingModelDic removeAllObjects];
NSMutableDictionary *indexDic = wself.preloadingModelDic; // 采用全局的字典 减少快速切换时 重复创建消耗性能的问题
indexDic[[NSString stringWithFormat:@"%d",leftCellIndex]] = @1;
indexDic[[NSString stringWithFormat:@"%d",model.index]] = @1;
indexDic[[NSString stringWithFormat:@"%d",rightCellIndex]] = @1;

for (NSString *indexStr in wself.loadingImageModelDic.allKeys) {
if (indexDic[indexStr]) continue;
LBScrollViewStatusModel *loadingModel = wself.loadingImageModelDic[indexStr];
if (loadingModel.opreation) {
[loadingModel.opreation cancel];
loadingModel.opreation = nil;
}
}
[wself.loadingImageModelDic removeAllObjects];
for (int i = leftCellIndex; i <= rightCellIndex; i++) {
model.shouldCancel = NO;
LBScrollViewStatusModel *loadingModel = wself.models[i];
NSString *indexStr = [NSString stringWithFormat:@"%d",i];
wself.loadingImageModelDic[indexStr] = loadingModel;
if (model.index == i) continue;
LBScrollViewStatusModel *preloadingModel = wself.models[i];
preloadingModel.shouldCancel = NO;
preloadingModel.currentPageImage = preloadingModel.currentPageImage ?:[wself getCacheImageForModel:preloadingModel];
if (preloadingModel.currentPageImage) continue;
[preloadingModel loadImage];
}

int finish = 0;
for (LBScrollViewStatusModel *obj in wself.models) {
if (obj.shouldCancel && obj.opreation) {
[obj.opreation cancel];
obj.opreation = nil;
}
if (obj.currentPageImage) {
finish = finish + 1;
}
}
if (finish == wself.models.count) {
wself.stopPreloading = YES;
}
});

}


Expand Down

0 comments on commit f0f8bac

Please sign in to comment.