OWPhotoBrowser 0.1.1

OWPhotoBrowser 0.1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2014年12月

未声明的所有者 维护。



  • 作者:
  • 吴建军

iOS 的轻量级图片浏览器。

功能

  • 使用 UIScrollView 的多图片浏览器
  • 支持图片弹出和动画消失
  • 支持图片缩放
  • 单击以关闭
  • 双击以重置图片缩放比例

演示

两个演示控制器

  • FlickrImagesViewController
  • LocalImagesViewController

屏幕截图

alt Photo dimissing alt Progress HUD

用法

点击缩略图以使用数据源创建图片浏览器

- (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

  • 在图片页面间添加间隔
  • 兼容 iOS 6.x

0.1.0

  • 支持缩放的图片浏览器
  • 支持图片弹出和消失动画
  • 简单演示