博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PlaceholderImageView
阅读量:4356 次
发布时间:2019-06-07

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

PlaceholderImageView

 

说明

1. PlaceHolderImageView基于SDWebImage编写

2. 给定一个图片的urlString,以及一个placeholderImage就可以优雅的显示图片加载效果

 

效果

 

源码

PlaceholderImageView.h/.m
////  PlaceholderImageView.h//  SDWebImageViewPlaceHorder////  Created by YouXianMing on 16/9/14.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import 
@interface PlaceholderImageView : UIView/** * Picture's url string. */@property (nonatomic, strong) NSString *urlString;/** * The placeholder's image. */@property (nonatomic, strong) UIImage *placeholderImage;/** * Default is UIViewContentModeScaleAspectFill. */@property (nonatomic) UIViewContentMode placeholderImageContentMode;/** * Default is UIViewContentModeScaleAspectFill. */@property (nonatomic) UIViewContentMode contentImageContentMode;@end
ImageCell.h/.m
////  ImageCell.h//  SDWebImageViewPlaceHorder////  Created by YouXianMing on 16/9/14.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import 
#import "PlaceholderImageView.h"@interface ImageCell : UITableViewCell@property (nonatomic, strong) PlaceholderImageView *placeholderImageView;@end
////  ImageCell.m//  SDWebImageViewPlaceHorder////  Created by YouXianMing on 16/9/14.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "ImageCell.h"@implementation ImageCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {                self.selectionStyle = UITableViewCellSelectionStyleNone;                self.placeholderImageView                  = [[PlaceholderImageView alloc] initWithFrame:CGRectMake(0, 0, 150.f, 150.f)];        self.placeholderImageView.placeholderImage = [UIImage imageNamed:@"1"];        [self addSubview:self.placeholderImageView];    }        return self;}@end
ViewController.m
////  ViewController.m//  SDWebImageViewPlaceHorder////  Created by YouXianMing on 16/9/14.//  Copyright © 2016年 YouXianMing. All rights reserved.//#import "ViewController.h"#import "UIImageView+WebCache.h"#import "PlaceholderImageView.h"#import "ImageCell.h"@interface ViewController () 
@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) NSArray *pictures;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.pictures = @[@"https://pic.cnblogs.com/avatar/607542/20151017230728.png", @"https://pic.cnblogs.com/face/607741/20140226202001.png", @"https://pic.cnblogs.com/face/815231/20150923160245.png", @"https://pic.cnblogs.com/face/993558/20160729092113.png", @"https://pic.cnblogs.com/face/894202/20160217151952.png", @"https://pic.cnblogs.com/face/968459/20160709111712.png", @"https://pic.cnblogs.com/face/145865/20160210174306.png", @"https://pic.cnblogs.com/face/796658/20151026090914.png", @"https://pic.cnblogs.com/face/933887/20160629214007.png", @"https://pic.cnblogs.com/face/125303/20130313094252.png", @"https://pic.cnblogs.com/face/976232/20160730173456.png", @"https://pic.cnblogs.com/face/969708/20160602120239.png", @"https://pic.cnblogs.com/face/954541/20160705113740.png", @"https://pic.cnblogs.com/face/799942/20150818204115.png"]; PlaceholderImageView *pImageView = [[PlaceholderImageView alloc] initWithFrame:CGRectMake(0, 0, 150.f, 150.f)]; [self.view addSubview:pImageView]; pImageView.urlString = @"https://pic.cnblogs.com/avatar/607542/20151017230728.png"; self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.rowHeight = 150.f; [self.view addSubview:self.tableView]; [self.tableView registerClass:[ImageCell class] forCellReuseIdentifier:@"ImageCell"];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _pictures.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCell"]; cell.placeholderImageView.urlString = _pictures[indexPath.row]; return cell;}@end

 

转载于:https://www.cnblogs.com/YouXianMing/p/5838549.html

你可能感兴趣的文章
《那些年啊,那些事——一个程序员的奋斗史》——54
查看>>
网页中css, js, img文件下载保存
查看>>
ubuntu cpus 共享打印
查看>>
docker-freebsd-20150625
查看>>
1、认识Struts2
查看>>
《机器学习技法》---soft-margin SVM
查看>>
java8系列
查看>>
Fedora设置中文
查看>>
HDU 4336 Card Collector:期望dp + 状压
查看>>
Linux 中变量$#,$0,$@,$*,$$,$?含义
查看>>
排球记分员计分程序(六)————Views视图的编码与设计
查看>>
Android-ndk-r8e wordlist 第二个参数不是数值参数
查看>>
PHP 预定义变量
查看>>
C++中指针和引用的区别比较
查看>>
Sybase存储过程例子
查看>>
关于 URI 和 URL 认识
查看>>
可以借鉴的阿里十面经验!
查看>>
利用linq快速判断给定数字是否包含在某个段范围内
查看>>
「NOI 2014」魔法森林
查看>>
指针循环错误实例
查看>>