SDWebImage 如何加載存儲在Ftp服務器上的圖片

問題引出

   最近公司做一個項目,爲了安全要求所有的圖片資源採用Ftp協議的方式訪問。iOS項目圖片緩存自然選擇了SDWebImage, 可是它如何加載Ftp協議的圖片呢?是不是一時間矇住了?其實很簡單。


解決辦法

只需要將用戶名與密碼封裝到圖片url裏即可。

NSString path = @"ftp://username:[email protected]/img/1.png";
[self.imgView sd_setImageWithURL:[NSURL URLWithString:path]];


示例項目

 開啓mac下 ftp服務器 


sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist  

同時也給出關閉ftp 服務器命令(現在不要關哦)
sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist   


在用戶路徑下建立一個img文件夾 放了三張圖片



建立一個示例iOS工程


cocopod 安裝SDWebImage  當前最新版本是 3.8.  地址是 https://github.com/rs/SDWebImage

pod init 後 打開 podfile 添加一行 



# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'FtpImageDemo' do
  # Uncomment the next line if you're using Swift or would like to use dynamic frameworks
  # use_frameworks!

  # Pods for FtpImageDemo
  pod 'SDWebImage', '~>3.8'

  target 'FtpImageDemoTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'FtpImageDemoUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end


執行pod install 





SDWebImge安裝成功


storyboard的控制器中 放入三個ImageView 連好線,寫如下代碼

#import "ViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView1;
@property (weak, nonatomic) IBOutlet UIImageView *imgView2;
@property (weak, nonatomic) IBOutlet UIImageView *imgView3;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self configViews];
    
}

- (void)configViews
{

    NSString *path1 = @"ftp://ruglcc:[email protected]/img/1.jpg";
    NSString *path2 = @"ftp://ruglcc:[email protected]/img/2.jpg";
    NSString *path3 = @"ftp://ruglcc:[email protected]/img/3.jpg";
    
    [self.imgView1 sd_setImageWithURL:[NSURL URLWithString:path1]];
    [self.imgView2 sd_setImageWithURL:[NSURL URLWithString:path2]];
    [self.imgView3 sd_setImageWithURL:[NSURL URLWithString:path3]];
}

編譯運行,有圖有真相








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章