使用第三方库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. Working With Taxonomy Field in CSOM

    How to create taxonomy field with CSOM If you need to programmatic create a taxonomy field, you need ...

  2. LINQ-to-SQL那点事~利用反射在LINQ-to-SQL环境中实现Ado.net的CURD操作

    回到目录 对于linq to sql提供的CURD操作,给我们的感觉就是简单,容易使用,更加面向对象,不用拼SQL语句了,这些好处都表示在处理单条实体或者集合长度小的情况下,如果有一个1000条的集合 ...

  3. atitit.ajax 最佳实践跟框架选型 o99

    atitit.ajax 最佳实践跟框架选型 1. 选型框架dwr/dwr3 跟jq 1 2. DWR方便的地方分为两个地方. 1 3. dwr 优点: 1 4. 缺点: 2 5. 根jq的区别 2 1 ...

  4. 固定表头/锁定前几列的代码参考[JS篇]

    引语:做有难度的事情,才是成长最快的时候.前段时间,接了一个公司的稍微大点的项目,急着赶进度,本人又没有独立带过队,因此,把自己给搞懵逼了.总是没有多余的时间来做自己想做的事,而且,经常把工作带入生活 ...

  5. javaweb学习总结(十四)——JSP原理

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  6. JDK版本不兼容问题之:一台机器安装多个版本的JDK

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://guojie.blog.51cto.com/59049/45964 我的机器上最开 ...

  7. FPGA4U FPGA SDRAM Controller

    -- https://fpga4u.epfl.ch/wiki/FPGA4U_Description -- The SDRAM bits data ..> signals, -- one ..&g ...

  8. Andorid--java0

    java code: public class Hello{     public static void main(String argv[]) {        System.out.printl ...

  9. C#之读取web上的xml

    一.使用LINQ读取使用Xdocument上的Load方法,可以快速的加载一个XML文档,然后使用LINQ对 加载XML文档进行查询或其他操作,这里仅简单偏历.所以,一旦查询一组元素有返回元素集,就可 ...

  10. HashMap工作原理(转载)

    转载自:http://www.importnew.com/7099.html  HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用Hash ...