IOS 使用SDWebImage实现仿新浪微博照片浏览器
使用第三方库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实现仿新浪微博照片浏览器的更多相关文章
- iOS开源照片浏览器框架SGPhotoBrowser的设计与实现
简介 近日在制作一个开源加密相册时附带着设计了一个照片浏览器,在进一步优化后发布到了GitHub供大家使用,该框架虽然没有MWPhotoBrowser那么强大,但是使用起来更为方便,操作更符合常规相册 ...
- ios 仿新浪微博 UINavigationController 向左滑动时显示上一个控制器的View.
仿新浪微博 UINavigationController 向左滑动时显示上一个控制器的View. 实现原理,UINavigationController 的 self.view显示时把当前显示的vie ...
- ASP.NET仿新浪微博下拉加载更多数据瀑布流效果
闲来无事,琢磨着写点东西.貌似页面下拉加载数据,瀑布流的效果很火,各个网站都能见到各式各样的展示效果,原理大同小异.于是乎,决定自己写一写这个效果,希望能给比我还菜的菜鸟们一点参考价值. 在开始之前, ...
- javascript仿新浪微博图片放大缩小及旋转效果
javascript仿新浪微博图片放大缩小及旋转效果 经常看到新浪微博里有图片放大缩小旋转效果,感觉效果还不错,所以就想试着做一个类似的demo出来,至于旋转对于IE可以用滤镜来解决,标准的浏览器可以 ...
- android的照片浏览器(一)至返回所有图片文件
今天开始写android的照片浏览器 首先要解决的问题是要得到sdcard下面所有是图片的文件的目录 于是我先写了一个普通的java类 来得到后缀是.jpg,.bmp.png.jpeg的文件 pack ...
- 监听文本框输入开发仿新浪微博限制输入字数的textarea插件
监听文本框输入 Firefox.Chrome.IE9,IE10 均支持 oninput 事件,此外所有版本的 IE 均支持 onpropertychange 事件. oninput 事件在用户输入.退 ...
- 照片浏览器软件-WTL开发的照片浏览器
前段时间,为了准备情人节礼物,本人想了做一个照片浏览器送给女友,专门播放我俩的所有照片的一个程序软件,于是,就写了这么一个照片浏览器软件.本程序是基于WTL8.0开发的一个图片/照片浏览器,涉及到XM ...
- 《转》iOS 平台 Cocos2d-x 项目接入新浪微博 SDK 的坑
最近在做一个 iOS 的 cocos2d-x 项目接入新浪微博 SDK 的时候被“坑”了,最后终于顺利的解决了.发现网上也有不少人遇到一样的问题,但是能找到的数量有限的解决办法写得都不详细,很难让人理 ...
- jQuery练手:仿新浪微博图片文字列表淡进淡出上下滚动效果
1.效果及功能说明 仿新浪微博图片文字列表上下淡进淡出间歇上下滚动 2.实现原理 首先要设定div内只能显示4个图片那么多出来的图片会自动隐藏然后在给图片添加一个动画的事件让他们可以滚动的播放出来上下 ...
随机推荐
- MVC3不能正确识别JSON中的Enum枚举值
一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...
- [ACM_图论] Highways (变形说法的最小生成树)
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28972#problem/C 题目给出T种情况,每种情况有n个城镇,接下来每一行是第i个城 ...
- [BTS] WCF-SAP adapter
=================================== Exception has been thrown by the target of an invocation. (mscor ...
- SQL——系统函数
1) CASE CASE有两种使用形式:一种是简单的CASE函数,另一种是搜索型的CASE函数. [1]简单的 CASE 函数 Format: CASE input_expression WHEN w ...
- javaweb学习总结(十)——HttpServletRequest对象(一)
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- Android 开发:view的几种布局方式及实践
View的几种布局显示方法,以后就不会在针对布局方面做过多的介绍.View的布局显示方式有下面几种:线性布局(Linear Layout).相对布局(Relative Layout).表格布局(Tab ...
- SqlServer 查看事务锁及执行语句
一.查看当前锁定的事务 ,) ,用户机器名称,) ,是否被锁住),blocked) ,数据库名称,),cmd 命令,waittype as 等待类型 ,last_batch 最后批处理时间,open_ ...
- Android之判断当前指定App是否在前台
直接上代码,不多说 //在进程中去寻找当前APP的信息,判断是否在前台运行 private boolean isAppOnForeground() { ActivityManager activity ...
- 使用PHP的CURL模拟POST采集开了viewstate的asp.net网页数据
用.NET做的网站如果做成POST提交方式,且开了viewstate的话,采集起来有点小繁琐,在此跟大家分享一下做法. 采的难点是必須先取得表單裏面的viewstate和datavalidtion兩個 ...
- 只写104行代码!在nopCommerce中如何实现自动生成网站地图
表告诉我说你不知道nopCommerce是什么.它是目前.NET中最流行的完全开源网上商城,由俄罗斯的团队在2008年开始立项一直开发到现在已经是3.3版本了.代码目前托管在codeplex上,有兴趣 ...