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个图片那么多出来的图片会自动隐藏然后在给图片添加一个动画的事件让他们可以滚动的播放出来上下 ...
随机推荐
- Android Message里传送的数据[转]
package org.hualang.handlertest; import android.app.Activity; import android.os.Bundle; import andro ...
- [python爬虫] Selenium定向爬取虎扑篮球海量精美图片
前言: 作为一名从小就看篮球的球迷,会经常逛虎扑篮球及湿乎乎等论坛,在论坛里面会存在很多精美图片,包括NBA球队.CBA明星.花边新闻.球鞋美女等等,如果一张张右键另存为的话真是手都点疼了.作为程序员 ...
- 1119 网页布局,css写下拉列表
<style type="text/css"> *{ margin:0px; padding:0px;} #body{ width:1000px; height:200 ...
- atitit.atitit.hb many2one relate hibernate 多对一关联配置..
atitit.atitit.hb many2one relate hibernate 多对一关联配置.. 1. 多对一单向 @ManyToOne 1 1. 其中@JoinColumn 注解 2 2. ...
- paip.java 调用c++ dll so总结
paip.java 调用c++ dll so总结 ///////JNA (这个ms sun 的) 我目前正做着一个相关的项目,说白了JNA就是JNI的替代品,以前用JNI需要编译一层中间库,现在JNA ...
- vc个版本对应的vs版本
VC6VC7(2003)VC8(2005)VC9(2008)VC10(2010)VC11(2012)VC12(2013)VC14(2015)
- sqlserver自定义函数的创建与调用
sqlserver中有系统提供的函数,像avg.sum.getdate()等,用户还可以自定义函数. 用户自定义的函数包括:标量函数和表值函数,其中标量函数和系统函数的用法一样,表值函数根据主体的定义 ...
- FGPA 双向 IO 自动方向控制
Using a Virtex Device to Drive 5V CMOS-Level Signals Voltage Level-Shifter Output Waveform
- RFID 基础/分类/编码/调制/传输
不同频段的RFID产品会有不同的特性,本文详细介绍了无源的感应器在不同工作频率产品的特性以及主要的应用. 目前定义RFID产品的工作频率有低频.高频和甚高频的频率范围内的符合不同标准的不同的产品,而且 ...
- go语言实现一个简单的登录注册web小程序
最近学习golang也有一段时间了,基础差不多学了个大概,因为本人是java程序员,所以对web更感兴趣.根据<go web编程>中的例子改编一个更简单的例子,供新手参考,废话不多说,上菜 ...