您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

用 PlaceholderImageView 打造優雅的圖片載入效果

編輯:iOS 開發綜合

設計說明

  1. PlaceholderImageView 是基於 SDWebImage 封裝的元件。
  2. 只要給它一個圖片網址字串與一張佔位圖,就能得到平順、無閃動的圖片載入過渡。

實際效果

PlaceholderImageView.h

//
//  PlaceholderImageView.h
//  SDWebImageViewPlaceHorder
//
//  Created by YouXianMing on 16/9/14.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//
#import <UIKit/UIKit.h>

@interface PlaceholderImageView : UIView
/** 圖片的網址字串 */
@property (nonatomic, strong) NSString *urlString;
/** 佔位圖片 */
@property (nonatomic, strong) UIImage *placeholderImage;
/** 佔位圖的顯示模式,預設 UIViewContentModeScaleAspectFill */
@property (nonatomic) UIViewContentMode placeholderImageContentMode;
/** 載入後圖片的顯示模式,預設 UIViewContentModeScaleAspectFill */
@property (nonatomic) UIViewContentMode contentImageContentMode;
@end

ImageCell 封裝

//
//  ImageCell.h
//
#import <UIKit/UIKit.h>
#import "PlaceholderImageView.h"

@interface ImageCell : UITableViewCell
@property (nonatomic, strong) PlaceholderImageView *placeholderImageView;
@end
//
//  ImageCell.m
//
#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 中的用法

使用方式非常直觀:準備一份網址清單,在 cellForRowAtIndexPath: 裡把網址丟給 urlString,剩下的載入、佔位與淡入都交給元件處理。

//
//  ViewController.m
//
#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"
    ];

    // 也可以單獨使用,不放在 cell 裡
    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

為什麼值得這樣封裝

直接用 SDWebImage 時,每個頁面都要重複寫「先設 placeholder、再設 imageWithURL、處理失敗狀態」這三段程式。把它收進一個 view 子類之後,呼叫端只剩一行賦值,介面語意也清楚得多。這類「薄封裝」在多人協作的專案中尤其能降低出錯機率。

以上就是使用 PlaceholderImageView 實現優雅圖片載入效果的完整說明,若有疑問歡迎留言,我會盡快回覆。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved