首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLSessionDownloadTask代表不解雇

NSURLSessionDownloadTask代表不解雇
EN

Stack Overflow用户
提问于 2014-02-12 11:11:17
回答 2查看 4.5K关注 0票数 3

我在玩NSURLSession的教程。我可以成功下载一张图片,但是代表下载的进度和下载完成都没有触发。以下是代码:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];


    NSString * imageUrl = @"http://ichef.bbci.co.uk/naturelibrary/images/ic/credit/640x395/r/ro/rock_pigeon/rock_pigeon_1.jpg";

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession * session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

    //Download image.
    NSURLSessionDownloadTask * getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]

                                               completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

                                                   if (error) {
                                                       NSLog(@"Error sadly for you is %@", [error localizedDescription]);
                                                   }

                                                   UIImage * downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];



                                                   dispatch_async(dispatch_get_main_queue(), ^ {
                                                       self.imageView.image = downloadedImage;
                                                   });

                                               }];

    [getImageTask resume];

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
    NSLog(@"Temporary File :%@\n", location);
    NSError *err = nil;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"out1.zip"]];
    if ([fileManager moveItemAtURL:location
                             toURL:docsDirURL
                             error: &err])
    {
        NSLog(@"File is saved to =%@",docsDir);
    }
    else
    {
        NSLog(@"failed to move: %@",[err userInfo]);
    }

}

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
    //You can get progress here
    NSLog(@"Received: %lld bytes (Downloaded: %lld bytes)  Expected: %lld bytes.\n",
          bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
}

在.h文件中:

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface SGGViewController : UIViewController <NSURLSessionDelegate> {
    IBOutlet UIImageView * imageView;
}

@property (nonatomic, strong) IBOutlet UIImageView * imageView;

@end

有人能建议怎么解决吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-12 17:44:49

您已经有了一个委托,所以您可以跳过任务创建的completionHandler/block形式,然后全部进入委托。

快速浏览一下holy script并没有告诉我任何权威的信息,说明指定一个完成处理程序是否会阻止激发委托方法,但是这种关系有很多是相互排斥的。

如果您还没有,我想您应该添加–URLSession:task:didCompleteWithError:到您的代表。它可能捕获纯下载委托方法可能遗漏的问题。

票数 5
EN

Stack Overflow用户

发布于 2014-02-12 12:27:38

现在使用NSUrlRequest,委托将调用。希望这能起作用

代码语言:javascript
复制
 - (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLSessionDownloadTask *downloadTask =nil;
    NSString * imageUrl = @"http://fc05.deviantart.net/fs71/i/2012/180/8/f/ios_6_logo_psd___png_by_theintenseplayer-d55eje9.png";
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession * session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]] ;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];

    downloadTask = [session downloadTaskWithRequest:request];
    [downloadTask resume ];

    /*
    NSURLSessionDownloadTask * getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]

                                                         completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

                                                             if (error) {
                                                                 NSLog(@"Error sadly for you is %@", [error localizedDescription]);
                                                             }

                                                             UIImage * downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]];



                                                             dispatch_async(dispatch_get_main_queue(), ^ {
                                                                 //self.imageView.image = downloadedImage;
                                                             });

                                                         }];

    [getImageTask resume];
     */

    // Do any additional setup after loading the view, typically from a nib.
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21726270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档