iOS 利用AFNetworking下載帶有多個參數(shù)的圖片解決方案

在某個項目中圖片下載并不是只有一個URL參數(shù),還附帶了其他的參數(shù),因此自己利用AFNetworking下載圖片,流程大致如下圖:

在封裝的網(wǎng)絡(luò)請求中提供一個方法,在方法中根據(jù)傳遞過來的URL,判斷在沙盒中是否存在這張圖片,存在直接返回,不存在調(diào)用網(wǎng)絡(luò)請求下載圖片。下載成功存儲在沙盒中,并返回給View中顯示。

第一步在xxx.h文件中提供一個方法,例如如下方法

- (nullable UIImage*)productResource:(nonnull NSString*)resourceId;

第二步在xxx.m文件中實現(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文件中實現(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;
    }
}

到此基本就完成了,不過這其中還有很多地方需要優(yōu)化,比如下載過程中中斷處理、圖片過大等等。還需要不斷的完善,僅提供一個處理思路。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容