iOS Developerios实用开发技巧

iOS 利用AFNetworking下载带有多个参数的图片解决

2017-03-20  本文已影响343人  LJ_影子

在某个项目中图片下载并不是只有一个URL参数,还附带了其他的参数,因此自己利用AFNetworking下载图片,流程大致如下图:

在封装的网络请求中提供一个方法,在方法中根据传递过来的URL,判断在沙盒中是否存在这张图片,存在直接返回,不存在调用网络请求下载图片。下载成功存储在沙盒中,并返回给View中显示。

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

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

第二步在xxx.m文件中实现该方法

- (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文件中提供获取沙盒图片方法和下载图片方法,以下面方法为例

// 根据相关参数下载图片
- (void)getResourceBy:(nonnull NSDictionary*)dictionary andBy:(nonnull NSString*)resourceId;
// 从沙盒中获取图片
- (nullable UIImage*)localImageById:(nonnull NSString*)resourceId;

第四步在ResourceManager.m文件中实现定义的方法

- (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;
    }
}

到此基本就完成了,不过这其中还有很多地方需要优化,比如下载过程中中断处理、图片过大等等。还需要不断的完善,仅提供一个处理思路。

上一篇下一篇

猜你喜欢

热点阅读