iOS程序開發(fā)之使用PlaceholderImageView實(shí)現(xiàn)優(yōu)雅的圖片加載效果

說明
1. PlaceHolderImageView基于SDWebImage編寫
2. 給定一個圖片的urlString,以及一個placeholderImage就可以優(yōu)雅的顯示圖片加載效果
效果

源碼
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程序開發(fā)之使用PlaceholderImageView實(shí)現(xiàn)優(yōu)雅的圖片加載效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 改變iOS應(yīng)用中UITableView的背景顏色與背景圖片的方法
- iOS開發(fā)中UIImageView控件的常用操作整理
- iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件
- IOS UI學(xué)習(xí)教程之使用UIImageView控件制作動畫
- iOS UIImageView圖片自動拉伸功能
- 詳解IOS UITableViewCell 的 imageView大小更改
- IOS中UIImageView方法實(shí)現(xiàn)簡單動畫
- IOS 中UIImageView響應(yīng)點(diǎn)擊事件
- iOS如何固定UITableView中cell.imageView.image的圖片大小
相關(guān)文章
iOS開發(fā)之圖片模糊效果的五種實(shí)現(xiàn)代碼
本篇文章主要介紹了iOS開發(fā)之模糊效果的五種實(shí)現(xiàn)代碼。本文針對這五種方式講解一下具體的實(shí)現(xiàn),有興趣的同學(xué)可以一起來了解一下2017-04-04
Objective-C基礎(chǔ) 自定義對象歸檔詳解及簡單實(shí)例
iOS統(tǒng)計(jì)代碼總行數(shù)的命令(便捷且簡單)

