iOS程序开发之使用PlaceholderImageView实现优雅的图片加载效果
程序员文章站
2023-12-14 13:07:28
说明
1. placeholderimageview基于sdwebimage编写
2. 给定一个图片的urlstring,以及一个placeholderim...
说明
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 <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
// // 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"
@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 () <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实现优雅的图片加载效果,希望对大家有所帮助