zoukankan      html  css  js  c++  java
  • 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(0, 0, 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 - 100) / 2, UISCREEN_HEIGHT - 50, 100, 25)];
            _pageIndexLabel.textAlignment = NSTextAlignmentCenter;
            _pageIndexLabel.textColor = [UIColor whiteColor];
            _pageIndexLabel.font = [UIFont fontWithName:@"Arial" size:20];
        }
        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, 0);
        self.backgroundScrollView.contentOffset = CGPointMake(currentSelectedIamge * UISCREEN_WIDTH, 0);
        for (int i = 0; i < [self.bigImageArray count]; i++) {
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:[self.bigImageArray objectAtIndex:i] ] options:0
                                                           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, 0, 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(0, 0, UISCREEN_WIDTH, UISCREEN_WIDTH * ratio);
                 weibImageView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT / 2);
                 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 + 1, [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 - 50) / 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 = 0; i < n; i++) {
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:[_imageArray objectAtIndex:i]
                                                                                          ] options:0
                                                           progress:^(NSInteger receivedSize, NSInteger expectedSize)
             {
                 NSLog(@"progress!!!");
             }
             
                                                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL)
             {
                 CGRect rect = CGRectMake(15 + (i % 3) * (imageWidth + 10) , 20 + 90 + (i / 3) * (imageWidth + 10), 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(0, 0, 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



  • 相关阅读:
    zhuan:Jmeter基础之---jmeter基础概念
    zhuan:JMeter基础之--元件的作用域与执行顺序
    zhuan:JMeter基础之—录制脚本
    zhuan:一种简单的数据库性能测试方法
    转:使用JMeter创建FTP测试计划
    转:JMeter基础之一 一个简单的性能测试
    性能学习
    [转]JMeter学习(一)工具简单介绍
    后端文件接收resd()和chunk的区别
    PyQt环境安装
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/5814812.html
Copyright © 2011-2022 走看看