• iOS UI 16 图片加载第三方 SDwebimage


    //

    //  RootViewController.m

    //  UI - 16 图片加载

    //

    //  Created by dllo on 15/11/30.

    //  Copyright (c) 2015 dllo. All rights reserved.

    //


    #import "RootViewController.h"

    #import "UIImageView+WebCache.h"

    #warning   1 加入头文件

    @interface RootViewController ()

    @property (nonatomic, retain)UIImageView *imagev;

    @end


    @implementation RootViewController

    - (void)dealloc

    {

        [_imagev release];

        [super dealloc];

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.view.backgroundColor = [UIColor whiteColor];

        

        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];

        but.frame = CGRectMake(50, 500, 80, 50);

        but.backgroundColor = [UIColor purpleColor];

        [but setTitle:@"下载图片" forState:UIControlStateNormal];

        [but addTarget:self action:@selector(butAct) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:but];

        

        

        

        

        

    #warning 2 创建UIImageView

        self.imagev = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 300, 200)];

        [self.view addSubview:self.imagev];

        [self.imagev release];

        

    #warning 3 打开buliding prease 选第二个 , 选中sd的所有方法(不是的有3), 点回车, 在弹出框内输入-fobjc-arc 回车 .

        

    //     NSString *urlstr = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";

    //    NSURL *url = [NSURL URLWithString:urlstr];

    //    //sd_ webImage方法(第一次下载,以后从本地取)

    //    [self.imagev sd_setImageWithURL:url];

        

        //使用自己的方法

    //    [imagev sd_setImageWithURLStr:@"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg"];

        

        //图片下载前给了个假图片

    //    [self.imagev sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"20.jpg"]];

        

        //sd_ webImage 通过key值找,key就是我们的url,先从缓存找

        

        

        //失败后重试

    //    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,

        

        // Do any additional setup after loading the view.

    }

    - (void)butAct

    {

        NSString *urlstr = @"http://img4.duitang.com/uploads/item/201207/28/20120728105310_jvAjW.thumb.600_0.jpeg";

            NSURL *url = [NSURL URLWithString:urlstr];

        //放个假图, 拼命加载

    //    [self.imagev sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"20.jpg"]];

        

        [self.imagev sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"20.jpg"] options: SDWebImageCacheMemoryOnly];

        //缓存大小(图片)

        [[SDWebImageManager sharedManager].imageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {

            

            NSLog(@"缓存空间大小:%ld,总大小:%ld",fileCount, totalSize);

        }];

        //清除内存缓存

        [[SDWebImageManager sharedManager].imageCache clearMemory];

        

        //清除磁盘缓存

        [[SDWebImageManager sharedManager].imageCache clearDisk];

    #warning clear不是clean

        

        //ios7 以后还要加清除网络请求的缓存

        [[NSURLCache sharedURLCache] removeAllCachedResponses];

        

        

        

        

    }

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }


    /*

    #pragma mark - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        // Get the new view controller using [segue destinationViewController].

        // Pass the selected object to the new view controller.

    }

    */


    @end


  • 相关阅读:
    C++ 类
    百练3383:Cell Phone Network
    百练4092:牛仔裤
    百练4091:最近餐馆
    百练4090:超级备忘录
    百练2868:牛顿迭代
    百练2812:恼人的青蛙
    百练2805:正方形
    bzoj1856 [Scoi2010]字符串
    bzoj2096 [Poi2010]Pilots
  • 原文地址:https://www.cnblogs.com/yuhaojishuboke/p/5043073.html
Copyright © 2020-2023  润新知