使用第三方库SDWebImage实现仿新浪微博照片浏览器,可以下载图片缓存,点击之后滚动查看相片,具体效果如下:    

代码如下:

WeiboImageView.h:

#import <UIKit/UIKit.h>

@interface WeiboImageView : UIImageView

@property (nonatomic, assign) CGRect originRect;

- (instancetype)initWithFrame:(CGRect)frame;

@end

WeiboImageView.m

#import "WeiboImageView.h"

@implementation WeiboImageView

@synthesize originRect;

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
originRect = frame;
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
self.contentMode = UIViewContentModeScaleAspectFill; }
return self;
} - (void)dealloc
{ } @end

WeiboImageBrowser.h

#import <UIKit/UIKit.h>

@interface WeiboImageBrowser : UIView<UIScrollViewDelegate>

//@property (nonatomic, copy) NSArray *imageViewArray;

@property (nonatomic, assign) int currentSelectedIamge;

@property (nonatomic, strong) UIScrollView *backgroundScrollView;

@property (nonatomic, strong) UILabel *pageIndexLabel;

@property (nonatomic,strong) NSMutableArray *originRects;

@property (nonatomic, copy) NSArray *bigImageArray;

- (instancetype)initWithFrame : (CGRect)rect;

- (void)showWeiboImages;

@end

WeiboImageBrowser.m

#import "WeiboImageBrowser.h"
#import "WeiboDefine.h"
#import "WeiboImageView.h"
#import "SDImageCache.h" //缓存相关
#import "SDWebImageCompat.h" //组件相关
#import "SDWebImageDecoder.h" //解码相关 //图片下载以及下载管理器
#import "SDWebImageDownloader.h"
#import "SDWebImageManager.h"
#import "SDWebImageDownloaderOperation.h" @implementation WeiboImageBrowser //@synthesize imageViewArray; @synthesize currentSelectedIamge; @synthesize bigImageArray; - (instancetype)initWithFrame : (CGRect)rect
{
self = [super initWithFrame:rect];
if (self) {
[self addSubview:self.pageIndexLabel];
self.backgroundColor = [UIColor blackColor];
[self initBackgroundScrollView];
}
return self;
} - (UIScrollView *)backgroundScrollView
{
if (!_backgroundScrollView) {
_backgroundScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , UISCREEN_WIDTH, UISCREEN_HEIGHT)];
_backgroundScrollView.pagingEnabled = YES;
}
return _backgroundScrollView;
} - (NSMutableArray *)originRects
{
if (!_originRects) {
_originRects = [NSMutableArray array];
}
return _originRects;
} - (UILabel *)pageIndexLabel
{
if (!_pageIndexLabel) {
_pageIndexLabel = [[UILabel alloc]initWithFrame:CGRectMake((UISCREEN_WIDTH - ) / , UISCREEN_HEIGHT - , , )];
_pageIndexLabel.textAlignment = NSTextAlignmentCenter;
_pageIndexLabel.textColor = [UIColor whiteColor];
_pageIndexLabel.font = [UIFont fontWithName:@"Arial" size:];
}
return _pageIndexLabel;
} - (void)initBackgroundScrollView
{
self.backgroundScrollView.contentSize = CGSizeMake([self.bigImageArray count] * UISCREEN_WIDTH, UISCREEN_HEIGHT);
self.backgroundScrollView.delegate = self;
self.backgroundScrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:self.backgroundScrollView];
} - (void)initImageScrollViews
{
self.backgroundScrollView.contentSize = CGSizeMake([self.bigImageArray count] * UISCREEN_WIDTH, );
self.backgroundScrollView.contentOffset = CGPointMake(currentSelectedIamge * UISCREEN_WIDTH, );
for (int i = ; i < [self.bigImageArray count]; i++) {
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:[self.bigImageArray objectAtIndex:i] ] options:
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
NSLog(@"progress!!!");
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)
{
UIScrollView *iamgeScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH * i, , UISCREEN_WIDTH, UISCREEN_HEIGHT)];
WeiboImageView *weibImageView = [[WeiboImageView alloc]init];
weibImageView.image = image;
CGFloat ratio = (double)weibImageView.image.size.height / (double)weibImageView.image.size.width;
weibImageView.bounds = CGRectMake(, , UISCREEN_WIDTH, UISCREEN_WIDTH * ratio);
weibImageView.center = CGPointMake(UISCREEN_WIDTH / , UISCREEN_HEIGHT / );
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)];
[weibImageView addGestureRecognizer:tap];
[iamgeScrollView addSubview:weibImageView];
[self.backgroundScrollView addSubview:iamgeScrollView];
}];
}
} - (void)showWeiboImages
{
[self initImageScrollViews];
[[UIApplication sharedApplication].keyWindow addSubview:self];
} -(void)imageTap:(UITapGestureRecognizer *)tap
{
[self removeFromSuperview];
} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int nCurrentPage = scrollView.contentOffset.x / UISCREEN_WIDTH;
currentSelectedIamge = nCurrentPage;
NSString *stringPage = [NSString stringWithFormat:@"%d/%d",nCurrentPage + , [self.bigImageArray count]];
self.pageIndexLabel.text = stringPage;
[self bringSubviewToFront:self.pageIndexLabel];
} @end

使用方法:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (nonatomic, strong)NSArray *imageArray;

@end

ViewController.m

#import "ViewController.h"
#import "SDImageCache.h" //缓存相关
#import "SDWebImageCompat.h" //组件相关
#import "SDWebImageDecoder.h" //解码相关 //图片下载以及下载管理器
#import "SDWebImageDownloader.h"
#import "SDWebImageManager.h"
#import "SDWebImageDownloaderOperation.h"
#import "WeiboDefine.h"
#import "WeiboImageView.h"
#import "WeiboImageBrowser.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
float imageWidth = (UISCREEN_WIDTH - ) / 3.0;
_imageArray = [NSArray arrayWithObjects:
@"http://g.hiphotos.baidu.com/image/pic/item/c2cec3fdfc03924578c6cfe18394a4c27c1e25e8.jpg",
@"http://image.tianjimedia.com/uploadImages/2015/072/23/4X4L0E9BNEZ6.jpg",
@"http://img15.3lian.com/2015/c1/83/d/31.jpg",
@"http://img15.3lian.com/2015/c1/83/d/31.jpg",
@"http://img15.3lian.com/2015/c1/83/d/31.jpg",
@"http://img15.3lian.com/2015/c1/83/d/31.jpg",
@"http://img15.3lian.com/2015/c1/83/d/31.jpg",
@"http://img15.3lian.com/2015/c1/83/d/31.jpg",
@"http://img15.3lian.com/2015/c1/83/d/31.jpg",nil ] ;
int n = _imageArray.count; for (int i = ; i < n; i++) {
[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:[_imageArray objectAtIndex:i]
] options:
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
NSLog(@"progress!!!");
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)
{
CGRect rect = CGRectMake( + (i % ) * (imageWidth + ) , + + (i / ) * (imageWidth + ), imageWidth, imageWidth);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect];
imageView.image = image;
imageView.frame = rect;
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.tag = i;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)];
[imageView addGestureRecognizer:tap];
[self.view addSubview:imageView]; }];
} // Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
} -(void)imageTap:(UITapGestureRecognizer *)tap
{
WeiboImageBrowser *imageBrowser = [[WeiboImageBrowser alloc] initWithFrame:CGRectMake(, , UISCREEN_WIDTH, UISCREEN_HEIGHT)];
imageBrowser.currentSelectedIamge = tap.view.tag;
imageBrowser.bigImageArray = /*weiboInformation.pic_urls*/_imageArray;
[imageBrowser showWeiboImages];
} @end

源代码下载链接:http://download.csdn.net/detail/lzm2625347497/9615067

IOS 使用SDWebImage实现仿新浪微博照片浏览器的更多相关文章

  1. iOS开源照片浏览器框架SGPhotoBrowser的设计与实现

    简介 近日在制作一个开源加密相册时附带着设计了一个照片浏览器,在进一步优化后发布到了GitHub供大家使用,该框架虽然没有MWPhotoBrowser那么强大,但是使用起来更为方便,操作更符合常规相册 ...

  2. ios 仿新浪微博 UINavigationController 向左滑动时显示上一个控制器的View.

    仿新浪微博 UINavigationController 向左滑动时显示上一个控制器的View. 实现原理,UINavigationController 的 self.view显示时把当前显示的vie ...

  3. ASP.NET仿新浪微博下拉加载更多数据瀑布流效果

    闲来无事,琢磨着写点东西.貌似页面下拉加载数据,瀑布流的效果很火,各个网站都能见到各式各样的展示效果,原理大同小异.于是乎,决定自己写一写这个效果,希望能给比我还菜的菜鸟们一点参考价值. 在开始之前, ...

  4. javascript仿新浪微博图片放大缩小及旋转效果

    javascript仿新浪微博图片放大缩小及旋转效果 经常看到新浪微博里有图片放大缩小旋转效果,感觉效果还不错,所以就想试着做一个类似的demo出来,至于旋转对于IE可以用滤镜来解决,标准的浏览器可以 ...

  5. android的照片浏览器(一)至返回所有图片文件

    今天开始写android的照片浏览器 首先要解决的问题是要得到sdcard下面所有是图片的文件的目录 于是我先写了一个普通的java类 来得到后缀是.jpg,.bmp.png.jpeg的文件 pack ...

  6. 监听文本框输入开发仿新浪微博限制输入字数的textarea插件

    监听文本框输入 Firefox.Chrome.IE9,IE10 均支持 oninput 事件,此外所有版本的 IE 均支持 onpropertychange 事件. oninput 事件在用户输入.退 ...

  7. 照片浏览器软件-WTL开发的照片浏览器

    前段时间,为了准备情人节礼物,本人想了做一个照片浏览器送给女友,专门播放我俩的所有照片的一个程序软件,于是,就写了这么一个照片浏览器软件.本程序是基于WTL8.0开发的一个图片/照片浏览器,涉及到XM ...

  8. 《转》iOS 平台 Cocos2d-x 项目接入新浪微博 SDK 的坑

    最近在做一个 iOS 的 cocos2d-x 项目接入新浪微博 SDK 的时候被“坑”了,最后终于顺利的解决了.发现网上也有不少人遇到一样的问题,但是能找到的数量有限的解决办法写得都不详细,很难让人理 ...

  9. jQuery练手:仿新浪微博图片文字列表淡进淡出上下滚动效果

    1.效果及功能说明 仿新浪微博图片文字列表上下淡进淡出间歇上下滚动 2.实现原理 首先要设定div内只能显示4个图片那么多出来的图片会自动隐藏然后在给图片添加一个动画的事件让他们可以滚动的播放出来上下 ...

随机推荐

  1. [51单片机] TFT2.4彩屏3 [自制动画效果-滑块+吊钩]

    >_<:引脚和前面几个连接一样,这里做了一个实用的动画效果,模拟起重机的2维视图. #ifndef __ILI9325_H__ #define __ILI9325_H__ void ILI ...

  2. Java程序员的日常——《编程思想》一切都是对象

    今天终于看完了第一章,哈哈,万事开头难....刚开始被编程思想的第一章给蒙住了,讲一堆理论,没什么意思.从第二章开始,真正的开始讲解Java相关的内容,有了一定的开发经验后,再次阅读起来,感觉收获良多 ...

  3. paip.uapi 获取网络url内容html 的方法java php ahk c++ python总结.

    paip.uapi 获取网络url内容html 的方法java php ahk c++ python总结. 各种语言总结比较,脚本php.python果然是方便.简短,实用. uapi : get_w ...

  4. iscroll初体验

    引入 iscroll是什么?多的概念性让人看不懂的东西就不说了因为看了也可能看不明白,iscroll主要用于移动端设备,主要包括以下的应用场合: 缩放 拉动刷新 速度和性能提升 精确捕捉元素 自定义滚 ...

  5. MySQL数据库定义与操作语言

    文章为作者原创,未经许可,禁止转载.    -Sun Yat-sen University 冯兴伟 实验1.1 数据库定义 (1)实验目的 理解和掌握数据库DDL语言,能够熟练地使用SQL DDL语句 ...

  6. nginx是一个反向代理的软件

    nginx只是一个反向代理的软件,和语言无关,理论上支持任何Web平台,当然http://Asp.net也不例外,http://51aspx.com就是http://Asp.net开发的,前端暴漏的是 ...

  7. ASP.NET MVC中实现多个按钮提交的几种方法

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...

  8. madown标签说明

    1.删除线的用法 ~~这是删除线~~

  9. mysql-5.7.15-winx64免安装版配置

    1.拷到硬盘根目录下; 2.在 bin 平行目录下新建  data 文件夹: 3. 修改  my-default.ini 文件,添加 basedir = C:\mysql-5.7.15-winx64d ...

  10. 项“XXXXX.sln”已在选择的位置受源代码管理

    项“XXXXX.sln”已在选择的位置受源代码管理.如果尝试重新绑定已在 Microsoft Visual Studio 外部添加到源代码管理的项目,应使用“更改源代码管理”命令.如果是第一次添加此项 ...