在某個(gè)項(xiàng)目中圖片下載并不是只有一個(gè)URL參數(shù)棉胀,還附帶了其他的參數(shù),因此自己利用AFNetworking下載圖片夕冲,流程大致如下圖:
在封裝的網(wǎng)絡(luò)請(qǐng)求中提供一個(gè)方法弄砍,在方法中根據(jù)傳遞過(guò)來(lái)的URL批狱,判斷在沙盒中是否存在這張圖片谓娃,存在直接返回坎背,不存在調(diào)用網(wǎng)絡(luò)請(qǐng)求下載圖片替劈。下載成功存儲(chǔ)在沙盒中,并返回給View中顯示得滤。
第一步在xxx.h文件中提供一個(gè)方法陨献,例如如下方法
- (nullable UIImage*)productResource:(nonnull NSString*)resourceId;
第二步在xxx.m文件中實(shí)現(xiàn)該方法
- (nullable UIImage*)productResource:(nonnull NSString*)resourceId
{
UIImage* localImage = [[ResourceManager sharedManager] localImageById:resourceId];
if (localImage) {
#ifdef YDLL_DEBUG
NSLog(@"cached image is used, imageId: %@", resourceId);
#endif
return localImage;
}
NSDictionary* dictionary = [NSMutableDictionary dictionary];
[dictionary setValue:resourceId forKey:@"imageId"];
NSDictionary* requestDictionary = [RemoteDataResolver plainRequestFor:ENDPOINT_GETRESOURCE withDelegate:self withRequest:dictionary];
[[ResourceManager sharedManager] getResourceBy:requestDictionary andBy:resourceId];
return nil;
}
第三步在ResourceManager.h文件中提供獲取沙盒圖片方法和下載圖片方法,以下面方法為例
// 根據(jù)相關(guān)參數(shù)下載圖片
- (void)getResourceBy:(nonnull NSDictionary*)dictionary andBy:(nonnull NSString*)resourceId;
// 從沙盒中獲取圖片
- (nullable UIImage*)localImageById:(nonnull NSString*)resourceId;
第四步在ResourceManager.m文件中實(shí)現(xiàn)定義的方法
- (void)getResourceBy:(nonnull NSDictionary*)dictionary andBy:(nonnull NSString*)resourceId
{
[self.httpDownloadManager POST:[dictionary valueForKey:REMOTEDATARESOLVER_REQUESTURL_KEY]
parameters:[dictionary valueForKey:REMOTEDATARESOLVER_REQUESTBODY_KEY]
progress:^(NSProgress * _Nonnull uploadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[UIImage class]]) {
NSString* resourcePath = [self.imageRoot stringByAppendingPathComponent:resourceId];
NSData* imageData = UIImageJPEGRepresentation(responseObject, 1.0);
[imageData writeToFile:resourcePath options:NSAtomicWrite error:nil];
id<ModelDelegate> delegate = [dictionary valueForKey:REMOTEDATARESOLVER_DELEGATE_KEY];
[delegate didSucceedOn:[dictionary valueForKey:REMOTEDATARESOLVER_ENDPOINT_KEY]
withResponseObject:responseObject
withRequestObject:[dictionary valueForKey:REMOTEDATARESOLVER_REQUESTBODY_KEY]];
} else {
id<ModelDelegate> delegate = [dictionary valueForKey:REMOTEDATARESOLVER_DELEGATE_KEY];
[delegate didFailOn:[dictionary valueForKey:REMOTEDATARESOLVER_ENDPOINT_KEY]
withRequestObject:[dictionary valueForKey:REMOTEDATARESOLVER_REQUESTBODY_KEY]];
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
id<ModelDelegate> delegate = [dictionary valueForKey:REMOTEDATARESOLVER_DELEGATE_KEY];
[delegate didFailOn:[dictionary valueForKey:REMOTEDATARESOLVER_ENDPOINT_KEY]
withRequestObject:[dictionary valueForKey:REMOTEDATARESOLVER_REQUESTBODY_KEY]];
}
];
}
// 獲取沙盒中圖片
- (nullable UIImage*)localImageById:(nonnull NSString*)resourceId
{
NSString* resourcePath = [self.imageRoot stringByAppendingPathComponent:resourceId];
if ([[NSFileManager defaultManager] fileExistsAtPath:resourcePath]) {
NSData* imageData = [NSData dataWithContentsOfFile:resourcePath];
return [UIImage imageWithData:imageData];
} else {
return nil;
}
}
到此基本就完成了懂更,不過(guò)這其中還有很多地方需要優(yōu)化眨业,比如下載過(guò)程中中斷處理、圖片過(guò)大等等沮协。還需要不斷的完善坛猪,僅提供一個(gè)處理思路。