MDKImageCollection
一套结合iOS原生相册和微信等app优点的图片展示方案
特性:
手势下滑关闭
在 dismiss 手势时,毛玻璃模糊度变化只支持 iOS10 以后
上滑唤出菜单
二维码识别,多个二维码时会弹出选择
支持在 IM 等聊天页面中跨信息切换原图
MDKImageDisplayController *display = [[MDKImageDisplayController alloc]initWithLargeClose:^NSString * _Nullable(MDKImageCloseOption * option, void (^ handler)(UIImage *)) {
利用option.index和option.lastIdentifier来返回一个identifer
异步加载image
handler(image);
}];
WebView 嵌入(仅支持 WKWebView,以下用 swift 展示)
webView.MDKImage.enableWhenClickImage {[weak self] (frame,imageURLArray,clickIndex) in
let display = MDKImageDisplayController(photoCount: imageURLArray.count, largeClose: { (option, handler) in
//下载图片image
handler(image)
})
display.setDisplayIndex(clickIndex)
if let nav = self?.navigationController{
display.transition.sourceScreenInset = UIEdgeInsets(top: nav.navigationBar.frame.maxY, left: 0, bottom: 0, right: 0)
}
display.registerAppearSourecFrame({ () -> (CGRect) in
return frame
})
display.registerDismissTargetFrame({ (option) -> (CGRect) in
if option.index == clickIndex{
return frame
}
return CGRect()//代表进行渐变过度
})
self?.present(display, animated: true, completion: nil)
}
计划中特性:
WebView 嵌入(已完成)- 读取时的占位提示(圆圈)
- 支持 GIF
支持视频- 是否支持 live photo(不确定能否实现,可能与视频一样无法实现)
- 使用 Metal 重新编写模糊效果以提升性能
简单入门:
pod ‘MDKImageCollection’
创建 MDKImageDisplayController
方法1,通过 index 来管理内容
MDKImageDisplayController *display = [[MDKImageDisplayController alloc]initWithPhotoCount:imageArr.count largeClose:^(MDKImageCloseOption * option, void (^ handler)(UIImage *)) {
handler(imageArr[option.index]);
}];
方法2,通过 identifier 来管理内容,例如假设 imageView.image 是打开的大图:
__weak typeof(self) _self = self;
MDKImageDisplayController *display = [[MDKImageDisplayController alloc]initWithLargeClose:^NSString * (MDKImageCloseOption * option, void (^ handler)(UIImage * image)) {
if (!option.lastIdentifier) {//首张打开的图片是没有lastIdentifier的
handler(view.image);
return identifer;
}else{
NSInteger lastIndex = [_self indexWithIdentifer:option.lastIdentifier];
if (option.index>0) {
if (lastIndex == 3) {
return nil;
}
lastIndex += 1;
handler([_self imageViewWithIndex:lastIndex].image);
return [_self identiferWithWithIndex:lastIndex];
}else{
if (lastIndex == 0) {
return nil;
}
lastIndex -= 1;
handler([_self imageViewWithIndex:lastIndex].image);
return [_self identiferWithWithIndex:lastIndex];
}
}
}];
注册缩放动画的来源 view:
__weak typeof(self) _self = self;
display.registerAppearSourecView = ^UIView *{
//获取出现时的来源view
return view;
};
display.registerDismissTargetView = ^UIView * (MDKImageCloseOption * option) {
//获取消失时的目标view
return [_self imageViewWithIndex:[_self indexWithIdentifer:option.lastIdentifier]];
};
关闭高斯模糊背景:
display.disableBlurBackgroundWithBlack = true;
由于从 3D touch present 出来不会走 transition 动画,如果要实现 3D touch,以下是临时解决办法:
在
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0);
中创建 MDKImageDisplayController *display; 后,设置
display.isFrom3DTouch = true;
以解决动画错误
完整代码查看DemoCtr