好库YBImageBrowser
1.原始方法
- 数据源
YBImageBrowseCellData
YBVideoBrowseCellData
YBImageBrowseCellData:
url:网络url
sourceObject:图片的对象 如果是nil 则没有还原动画和隐藏当前图片功能 是直上直下动画 也还行 😁
//本地建议用这个 Local image (配置本地图片推荐使用 YBImage)
data.imageBlock = ^__kindof UIImage * _Nullable{
return [YBImage imageNamed:imageStr];
};
phAsset:asset对象
YBVideoBrowseCellData:
url:网络url 和本地 本地把path转换成url
NSString *path = [[NSBundle mainBundle] pathForResource:@"" ofType:@""];
NSURL *url = [NSURL fileURLWithPath:path];
sourceObject:图片的对象 如果是nil 则没有还原动画和隐藏当前图片功能 是直上直下动画 也还行 😁
phAsset:asset对象
- 浏览器对象创建
browserDataArr 就是装载着YBImageBrowseCellData、YBVideoBrowseCellData的数组
YBImageBrowser *browser = [YBImageBrowser new];
browser.dataSourceArray = browserDataArr;
browser.currentIndex = index;
[browser show];
2.代理方法
YBImageBrowser *browser = [YBImageBrowser new];
browser.dataSource = self;
browser.currentIndex = index;
[browser show];
代理
YBImageBrowserDataSource, YBImageBrowserDelegate
- (NSUInteger)yb_numberOfCellForImageBrowserView:(YBImageBrowserView *)imageBrowserView {
return self.dataArray.count;
}
- (id<YBImageBrowserCellDataProtocol>)yb_imageBrowserView:(YBImageBrowserView *)imageBrowserView dataForCellAtIndex:(NSUInteger)index {
PHAsset *asset = (PHAsset *)self.dataArray[index];
if (asset.mediaType == PHAssetMediaTypeVideo) {
// Type 1 : 系统相册的视频 / Video of system album
YBVideoBrowseCellData *data = [YBVideoBrowseCellData new];
data.phAsset = asset;
data.sourceObject = [self sourceObjAtIdx:index];
return data;
} else if (asset.mediaType == PHAssetMediaTypeImage) {
// Type 2 : 系统相册的图片 / Image of system album
YBImageBrowseCellData *data = [YBImageBrowseCellData new];
data.phAsset = asset;
data.sourceObject = [self sourceObjAtIdx:index];
return data;
}
return nil;
}
3.普通例子
- (id)sourceObjAtIdx:(NSInteger)idx {
MainImageCell *cell = (MainImageCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
return cell ? cell.mainImageView : nil;
}
- (void)showBrowserForMixedCaseWithIndex:(NSInteger)index {
NSMutableArray *browserDataArr = [NSMutableArray array];
[self.dataArray enumerateObjectsUsingBlock:^(NSString *_Nonnull imageStr, NSUInteger idx, BOOL * _Nonnull stop) {
// 此处只是为了判断测试用例的数据源是否为视频,并不是仅支持 MP4。/ This is just to determine whether the data source of the test case is video, not just MP4.
if ([imageStr hasSuffix:@".MP4"]) {
if ([imageStr hasPrefix:@"http"]) {
// Type 1 : 网络视频 / Network video
YBVideoBrowseCellData *data = [YBVideoBrowseCellData new];
data.url = [NSURL URLWithString:imageStr];
data.sourceObject = [self sourceObjAtIdx:idx];
[browserDataArr addObject:data];
} else {
// Type 2 : 本地视频 / Local video
NSString *path = [[NSBundle mainBundle] pathForResource:imageStr.stringByDeletingPathExtension ofType:imageStr.pathExtension];
NSURL *url = [NSURL fileURLWithPath:path];
YBVideoBrowseCellData *data = [YBVideoBrowseCellData new];
data.url = url;
data.sourceObject = [self sourceObjAtIdx:idx];
[browserDataArr addObject:data];
}
} else if ([imageStr hasPrefix:@"http"]) {
// Type 3 : 网络图片 / Network image
YBImageBrowseCellData *data = [YBImageBrowseCellData new];
data.url = [NSURL URLWithString:imageStr];
data.sourceObject = [self sourceObjAtIdx:idx];
[browserDataArr addObject:data];
} else {
// Type 4 : 本地图片 / Local image (配置本地图片推荐使用 YBImage)
YBImageBrowseCellData *data = [YBImageBrowseCellData new];
data.imageBlock = ^__kindof UIImage * _Nullable{
return [YBImage imageNamed:imageStr];
};
data.sourceObject = [self sourceObjAtIdx:idx];
[browserDataArr addObject:data];
}
}];
//Type 5 : 自定义 / Custom
CustomCellData *data = [CustomCellData new];
data.text = @"Custom Cell";
[browserDataArr addObject:data];
YBImageBrowser *browser = [YBImageBrowser new];
browser.dataSourceArray = browserDataArr;
browser.currentIndex = index;
[browser show];
}
4.注意
1.currentIndex 一定要在dataSourceArray之后赋值 不然提示越界
5.self.yy_isDecodedForDisplay = YES self.isDecodedForDisplay = YES YYModel会报错
6.加入一个扩展就是一个回调@property(nonatomic,copy)void (^hideBlock)(void);
有时候banner跳转 回导致返回的图片跑到屏幕外,配合viewDidAppear进行暂停轮滑
7 iOS 14黑屏问题 是YYAnimatedImageView有问题
iOS14+Xcode12
搜索修改YYKit中的代码
- (void)displayLayer:(CALayer *)layer {
if (_curFrame) {
layer.contents = (__bridge id)_curFrame.CGImage;
} else {
//如果没有动画帧,请调用super实现。iOS 14+ UIImageView使用此委托方法进行渲染。
if ([UIImageView instancesRespondToSelector:@selector(displayLayer:)]) {
[super displayLayer:layer];
}
}
}