服务器之家

服务器之家 > 正文

iOS程序开发之使用PlaceholderImageView实现优雅的图片加载效果

时间:2021-01-25 16:06     来源/作者:YouXianMing

iOS程序开发之使用PlaceholderImageView实现优雅的图片加载效果

说明

1. placeholderimageview基于sdwebimage编写

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

效果

iOS程序开发之使用PlaceholderImageView实现优雅的图片加载效果

源码

placeholderimageview.h/.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// placeholderimageview.h
// sdwebimageviewplacehorder
//
// created by youxianming on 16/9/14.
// copyright © 2016年 youxianming. all rights reserved.
//
#import <uikit/uikit.h>
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// imagecell.h
// sdwebimageviewplacehorder
//
// created by youxianming on 16/9/14.
// copyright © 2016年 youxianming. all rights reserved.
//
#import <uikit/uikit.h>
#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"

?
1
2
3
4
5
6
7
8
9
10
11
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// 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 () <uitableviewdelegate, uitableviewdatasource>
@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

以上所述是小编给大家介绍的ios程序开发之使用placeholderimageview实现优雅的图片加载效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部