下载图像现在可以使用NSURLSessionDataTask

时间:2017-01-31 11:52:07

标签: objective-c uiimage ios9 nsurlconnection nsurlsessiondatatask

我正在将已弃用的iOS9代码迁移到iOS10,特别是:

@interface IconDownloader : NSObject<NSURLConnectionDataDelegate,NSURLConnectionDelegate>{
    NSMutableData *receivedData;
}
@property(nonatomic,strong)CapabilityType *capability;
@property(nonatomic,strong)CapabilitiesItemView *item;
-(void)downloadIcon:(CapabilityType *)capability forItem:(CapabilitiesItemView *)item;
@end

@implementation IconDownloader
.
.
.
.
NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self];
    [con start];

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"DEPRECATED - didReceiveData"); 
    if(!receivedData){
        receivedData=[[NSMutableData alloc] initWithData:data];
    }else
        [receivedData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"DEPRECATED - connectionDidFinishLoading"); 
    NSLog(@"NSURLConnection - COMPLETE, dataLength=%lu",(unsigned long)receivedData.length);
    NSLog(@"DataIOS7+: %@",receivedData);
    if(receivedData.length>0){
        UIImage *image=[UIImage imageWithData:receivedData];
        if(image){
            self.capability.icon=image;
            self.item.iconImageView.image=image;
        }
    }

这个已弃用但可靠的代码完美无缺。 当我尝试对iOS10 +进行改编时,图像变量总是初始化为null,而我的imageViews看起来是空白的:

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:req
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
                                      NSLog(@"NSURLSessionDataTask - COMPLETE, error=%@",error);
                                      if(!error){
                                          NSLog(@"NSURLSessionDataTask - COMPLETE, dataLength=%lu",(unsigned long)data.length);
                                          NSLog(@"DataIOS10: %@",data);
                                          if(data.length>0){
                                              NSData *dataIOS10 = [[NSData alloc] initWithBase64EncodedString:[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] options:NSDataBase64DecodingIgnoreUnknownCharacters];
                                              UIImage *image=[UIImage imageWithData:dataIOS10];

                                              dispatch_async(dispatch_get_main_queue(), ^{
                                              if(image){
                                                  NSLog(@"NSURLSessionDataTask - COMPLETE, IMAGE OK");
                                                  self.capability.icon=image;
                                                  self.item.iconImageView.image=image;
                                              }
                                              else{
                                                  NSLog(@"NSURLSessionDataTask - COMPLETE, IMAGE ERROR");
                                                  self.capability.icon=image;//Lets try shoving that data there, just in case LOL
                                                  self.item.iconImageView.image=image;
                                              }
                                              });
                                          }
                                      }
                                  }];
[dataTask resume];

EXTRA,请求似乎正常:

NSURLSessionDataTask - COMPLETE, response=<NSHTTPURLResponse: 0x15e450f0> { URL: https://abc.SOMESERVER.net/prod/wcid/iphone/icon_fun.png } { status code: 200, headers {
    "Accept-Ranges" = bytes;
    Connection = "keep-alive";
    "Content-Length" = 6416;
    "Content-Type" = "image/png";
    Date = "Tue, 31 Jan 2017 13:11:05 GMT";
    Etag = "\"e554f6246076dc4eddf589f9552608b5\"";
    "Last-Modified" = "Mon, 28 Apr 2014 13:30:15 GMT";
    Server = AmazonS3;
    Via = "1.1 dc81da318a4ae20e51ccfd9463219596.cloudfront.net (CloudFront)";
    "X-Amz-Cf-Id" = "xlj4_3IUrjctZLrGlegXoZbrmTYqd_ahcGzG3K2glG3m6keedPk6mQ==";
    "X-Cache" = "Miss from cloudfront";
    "x-amz-meta-cb-modifiedtime" = "Thu, 31 Oct 2013 15:26:16 GMT";
} }

尽管总是具有非零字节大小,并且有或没有那种花哨的Base64编码,图像将始终为空...它总是失败。我甚至尝试在主线程上运行代码。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

良好的起点是检查同步呼叫是否正常工作,即

NSData * imgData = [[NSData alloc] initWithContentsOfURL:url]; UIImage * image = [UIImage imageWithData:imgData];

如果您无法对数据进行成像,那么您的请求就会出现问题。