博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UI 16 图片加载第三方 SDwebimage
阅读量:6976 次
发布时间:2019-06-27

本文共 3419 字,大约阅读时间需要 11 分钟。

//

//  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 = @"";

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

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

//    [self.imagev sd_setImageWithURL:url];

    

    //使用自己的方法

//    [imagev sd_setImageWithURLStr:@""];

    

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

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

转载于:https://www.cnblogs.com/yuhaojishuboke/p/5043073.html

你可能感兴趣的文章
MongoDB安装
查看>>
我的新技术博客
查看>>
第三期 OSI七层中第一层 物理层
查看>>
No.6 PHP的基本配置与优化
查看>>
javabean属性的类型选择包装类还是基本数据类型
查看>>
使用T-SQL语句操作数据表-更新数据
查看>>
关于SAP BW提示“Carry out repairs in non-original only
查看>>
中国书法的造型元素与原理 刘彦湖
查看>>
20170507Linux七周二次课 io监控free ps 网络状态 抓包
查看>>
26期20180601目录管理
查看>>
26期20180716 iptables规则备份恢复 firewalld zone
查看>>
营销自动化为什么能吸引企业的喜欢?它有何魅力?
查看>>
网络分流器-网络分流器IP网络路由交换测试技术探讨
查看>>
部分人说 Java 的性能已经达到甚至超过 C++,是真的吗?
查看>>
网络安全技术分析:DDoS的攻与防
查看>>
LNMP安装配置
查看>>
什么是机器人底盘 答案在这里!
查看>>
SNMP 协议 OID的使用
查看>>
【CSS3教程】CSS3基础&常用技巧&实例集合
查看>>
面试题:2018最全Redis面试题整理
查看>>