iOS 的轻量级图片浏览器。
两个演示控制器
屏幕截图
点击缩略图以使用数据源创建图片浏览器
- (void)showFromView:(UIImageView *)view
{
OWCustomPhotosDataSource *datasource = [[OWCustomPhotosDataSource alloc] initWithImageURLs:gLargeImages];
datasource.thumbnailViews = self.imageViews;
OWPhotoBrowserController *photoBrowser = [[OWPhotoBrowserController alloc] initWithDataSource:datasource];
[photoBrowser setCurrentPage:[self.imageViews indexOfObject:view]];
photoBrowser.fromView = view;
[self presentViewController:photoBrowser
animated:view.image ? YES : NO
completion:NULL];
}
** 实现自定义 OWPhotoDataSource 协议
- (void)loadImageAtIndex:(NSUInteger)index
progress:(PhotoLoadProgress)progress
complete:(PhotoLoadComplete)complete
{
NSParameterAssert(index < self.imageURLs.count);
NSParameterAssert(complete);
NSURL *url = [NSURL URLWithString:self.imageURLs[index]];
if ([self.loadingConnections objectForKey:url]) {
return;
}
NSURLRequest *req = [NSURLRequest requestWithURL:url];
__weak typeof(self) weakSelf = self;
__block OWSimpleURLConnection * connection = nil;
connection = [OWSimpleURLConnection OW_asyncRequest:req progressHandler:^(NSUInteger totalBytes, NSUInteger receivedBytes) {
if (progress) {
progress(receivedBytes * 1.f / totalBytes);
}
} completionHandler:^(OWSimpleURLConnection *conn, NSData *data, NSError *connectionError) {
if (connectionError == nil && conn.httpResponse.statusCode == 200) {
connection = conn;
complete([[UIImage alloc] initWithData:data scale:[UIScreen mainScreen].scale], connectionError);
}
[weakSelf.loadingConnections removeObjectForKey:url];
}];
[connection start];
progress(0.f);
[self.loadingConnections setObject:connection forKey:url];
}
或使用 SDWebImage 实现数据源
- (void)loadImageAtIndex:(NSUInteger)index
progress:(PhotoLoadProgress)progress
complete:(PhotoLoadComplete)complete
{
NSParameterAssert(index < self.imageURLs.count);
NSParameterAssert(complete);
NSURL *url = [NSURL URLWithString:self.imageURLs[index]];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
__weak typeof(self) weakSelf = self;
__block id<SDWebImageOperation> op = nil;
op = [manager downloadWithURL:url
options:0
progress:^(NSInteger receivedSize,
NSInteger expectedSize) {
if (progress) {
progress(receivedSize * 1.f / expectedSize);
}
}
completed:^(UIImage *image,
NSError *error,
SDImageCacheType cacheType,
BOOL finished) {
if (finished) {
complete(image);
}
[weakSelf.loadingOperations removeObject:op];
}];
[self.loadingOperations addObject:op];
}
- (BOOL)imageExistedInCacheAtIndex:(NSUInteger)index
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
NSString *url = self.imageURLs[index];
if ([manager.imageCache imageFromMemoryCacheForKey:url] != nil) {
return YES;
} else if ([manager diskImageExistsForURL:[NSURL URLWithString:url]]) {
return YES;
} else {
return NO;
}
}
0.1.1
0.1.0