問題引出
最近公司做一個項目,爲了安全要求所有的圖片資源採用Ftp協議的方式訪問。iOS項目圖片緩存自然選擇了SDWebImage, 可是它如何加載Ftp協議的圖片呢?是不是一時間矇住了?其實很簡單。
解決辦法
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
sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist
在用戶路徑下建立一個img文件夾 放了三張圖片
建立一個示例iOS工程
# 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
#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]];
}