IOS第三方之SDWebImage

來源:互聯網
上載者:User

標籤:

項目中從伺服器端下載圖片這些幾乎是必備的,使用時也很簡單,只需引入SDWebImage檔案

////  ViewController.m//  sdWebImageDemo////  Created by City--Online on 15/6/15.//  Copyright (c) 2015年 City--Online. All rights reserved.//#import "ViewController.h"#import "UIImageView+WebCache.h"@interface ViewController ()@property(nonatomic,strong) UIImageView *imgView1;@property(nonatomic,strong) UIImageView *imgView2;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    UIButton *claerBtn=[UIButton buttonWithType:UIButtonTypeSystem];    [claerBtn setTitle:@"清除緩衝" forState:UIControlStateNormal];    claerBtn.frame=CGRectMake(20, 10, 100, 100);    [claerBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    claerBtn.tag=10001;    [self.view addSubview:claerBtn];        UIButton *loadBtn=[UIButton buttonWithType:UIButtonTypeSystem];    [loadBtn setTitle:@"載入" forState:UIControlStateNormal];    loadBtn.frame=CGRectMake(130, 10, 100, 100);    [loadBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];    loadBtn.tag=10002;    [self.view addSubview:loadBtn];        _imgView1=[[UIImageView alloc]initWithFrame:CGRectMake(20, 120, 250, 250)];    _imgView1.backgroundColor=[UIColor grayColor];    [self.view addSubview:_imgView1];        _imgView2=[[UIImageView alloc]initWithFrame:CGRectMake(20, 380, 250, 250)];    _imgView2.backgroundColor=[UIColor grayColor];    [self.view addSubview:_imgView2];        }-(void)btnClick:(id)sender{    UIButton *btn=(UIButton *)sender;    if (btn.tag==10001) {        [[SDImageCache sharedImageCache] clearDisk];                [[SDImageCache sharedImageCache] clearMemory];    }    else if (btn.tag==10002)    {        NSURL *imgPath1=[NSURL URLWithString:@"http://s15.sinaimg.cn/middle/9914f9fdhbc6170891ebe&690"];        NSURL *imgPath2=[NSURL URLWithString:@"http://s14.sinaimg.cn/middle/9914f9fdhbc611c219f3d&690"];                //基本使用方法//        [_imgView1 sd_setImageWithURL:imgPath1 ];//        //        //block//        [_imgView2 sd_setImageWithURL:imgPath2 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {//            NSLog(@"這裡可以在圖片載入完成之後做些事情");//        }];                //預先設定一張圖片//        [_imgView1 sd_setImageWithURL:imgPath1 placeholderImage:[UIImage imageNamed:@"default.jpg"]];//        //        //block 預先設定一張圖片//        [_imgView2 sd_setImageWithURL:imgPath2 placeholderImage:[UIImage imageNamed:@"default.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {//            NSLog(@"這裡可以在圖片載入完成之後做些事情");//        }];//        [_imgView1 sd_setImageWithURL:imgPath1 placeholderImage:[UIImage imageNamed:@"default.jpg"] options:SDWebImageCacheMemoryOnly];                SDWebImageManager *manager=[SDWebImageManager sharedManager];        [manager downloadImageWithURL:imgPath2 options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {            NSLog(@"%f",receivedSize/(float)expectedSize);                    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {            NSLog(@"下載完成");            _imgView2.image=image;        }];//                //    /*        //     //失敗後重試        //     SDWebImageRetryFailed = 1 << 0,        //        //     //UI互動期間開始下載,導致延遲下載比如UIScrollView減速。        //     SDWebImageLowPriority = 1 << 1,        //        //     //只進行記憶體緩衝        //     SDWebImageCacheMemoryOnly = 1 << 2,        //        //     //這個標誌可以漸進式下載,顯示的映像是逐步在下載        //     SDWebImageProgressiveDownload = 1 << 3,        //        //     //重新整理緩衝        //     SDWebImageRefreshCached = 1 << 4,        //        //     //後台下載        //     SDWebImageContinueInBackground = 1 << 5,        //        //     //NSMutableURLRequest.HTTPShouldHandleCookies = YES;        //        //     SDWebImageHandleCookies = 1 << 6,        //        //     //允許使用無效的SSL認證        //     //SDWebImageAllowInvalidSSLCertificates = 1 << 7,        //        //     //優先下載        //     SDWebImageHighPriority = 1 << 8,        //             //     //延遲預留位置        //     SDWebImageDelayPlaceholder = 1 << 9,        //             //     //改變動畫形象        //     SDWebImageTransformAnimatedImage = 1 << 10,        //     */    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

IOS第三方之SDWebImage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.