前因
項(xiàng)目里面用到了環(huán)信的 EaseUI 庫,而其中又包含了第三方庫的引用。MWPhotoBrowser 就是其中的一個(gè)。而 MWPhotoBrowser 自身又包含了第三方的依賴,而且上次更新已經(jīng)很早了。作者不維護(hù)了。
MWPhotoBrowser 依賴的第三方庫:
- MBProgressHUD
- 0.9 (最新:1.0.0)
- SDWebImage
- 3.7 (最新:4.0.0)
- DACircularProgress
- 2.3 (最新:2.3.1)
尤其是 SDWebImage 這么大眾的的庫,竟然沒有更新。不能忍。于是就想著 fork 后,更新下依賴,自用。
本文的內(nèi)容,大體上是這次更新中遇到的問題的總結(jié)。
修改源文件
fork 后,MWPhotoBrowser 會(huì)在自己的 github 項(xiàng)目下。
git clone 下來。
修改 MWPhotoBrowser.podspec 文件
主要修改三個(gè)地方:
- s.homepage (項(xiàng)目首頁地址)
- s.source(項(xiàng)目源地址)
- s.dependency (項(xiàng)目依賴的第三方庫,我們主要改動(dòng)的是這里)
修改的內(nèi)容如下 (可飄過)
Pod::Spec.new do |s|
s.name = 'MWPhotoBrowser'
s.version = '2.1.2'
s.license = 'MIT'
s.summary = 'A simple iOS photo and video browser with optional grid view, captions and selections.'
s.description = <<-DESCRIPTION
MWPhotoBrowser can display one or more images or videos by providing either UIImage
objects, PHAsset objects, or URLs to library assets, web images/videos or local files.
The photo browser handles the downloading and caching of photos from the web seamlessly.
Photos can be zoomed and panned, and optional (customisable) captions can be displayed.
DESCRIPTION
s.screenshots = [
'https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser1.png',
'https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser2.png',
'https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser3.png',
'https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser4.png',
'https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser5.png',
'https://raw.github.com/mwaterfall/MWPhotoBrowser/master/Screenshots/MWPhotoBrowser6.png'
]
//修改這里
s.homepage = 'https://github.com/EvoIos/MWPhotoBrowser'
s.author = { 'Michael Waterfall' => 'michaelwaterfall@gmail.com' }
s.social_media_url = 'https://twitter.com/mwaterfall'
//修改這里
s.source = {
:git => 'https://github.com/EvoIos/MWPhotoBrowser.git',
:tag => '2.1.2'
}
s.platform = :ios, '7.0'
s.source_files = 'Pod/Classes/**/*'
s.resource_bundles = {
'MWPhotoBrowser' => ['Pod/Assets/*.png']
}
s.requires_arc = true
s.frameworks = 'ImageIO', 'QuartzCore', 'AssetsLibrary', 'MediaPlayer'
s.weak_frameworks = 'Photos'
//修改這里
s.dependency 'MBProgressHUD', '~> 1.0'
s.dependency 'DACircularProgress', '~> 2.3.1'
# SDWebImage
s.dependency 'SDWebImage', '~> 4.0.0'
end
修改 MWPhoto.h
因?yàn)樾屡f版本語法的不同,所以需要手動(dòng)更改源項(xiàng)目的文件。目前,只有 SDWebImage 相關(guān)的需要修改。
頭文件引入 SDWebImage/SDWebImageDownloader.h
修改 downloadImageWithURL 方法,因?yàn)榕f版和新版的方法不一樣了,所以這里需要更改下。
// Load from local file
- (void)_performLoadUnderlyingImageAndNotifyWithWebURL:(NSURL *)url {
@try {
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
if (expectedSize > 0) {
float progress = receivedSize / (float)expectedSize;
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:progress], @"progress",
self, @"photo", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:MWPHOTO_PROGRESS_NOTIFICATION object:dict];
}
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
if (error) {
MWLog(@"SDWebImage failed to download image: %@", error);
}
_webImageOperation = nil;
self.underlyingImage = image;
dispatch_async(dispatch_get_main_queue(), ^{
[self imageLoadingComplete];
});
}];
} @catch (NSException *e) {
MWLog(@"Photo from web: %@", e);
_webImageOperation = nil;
[self imageLoadingComplete];
}
}
提交
更改完后,提交到 GitHub 上。然后就可以用了。
使用
使用的時(shí)候,有些不同。因?yàn)槲覀兪?fork 別人的庫,自己修改了,所以,不好傳到 pods 上公開使用。但是我們還想要使用改過的庫,怎么辦?
集成的時(shí)候,標(biāo)定遠(yuǎn)程項(xiàng)目庫的地址和 commit 的版本。至于為什么要有 commit 版本,stackOverflow 上有個(gè)提問,見這里。
最后 Podfile 里的文件內(nèi)容如下:
pod "MWPhotoBrowser", :git => 'https://github.com/EvoIos/MWPhotoBrowser.git', :commit => 'de697e101195557ddca85661ebb266fd3f10776c'
附錄
fork 的 MWPhotoBrowser 項(xiàng)目地址