我在玩NSURLSession的教程。我可以成功下载一张图片,但是代表下载的进度和下载完成都没有触发。以下是代码:
- (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文件中:
#import <UIKit/UIKit.h>
@interface SGGViewController : UIViewController <NSURLSessionDelegate> {
IBOutlet UIImageView * imageView;
}
@property (nonatomic, strong) IBOutlet UIImageView * imageView;
@end有人能建议怎么解决吗?
发布于 2014-02-12 17:44:49
您已经有了一个委托,所以您可以跳过任务创建的completionHandler/block形式,然后全部进入委托。
快速浏览一下holy script并没有告诉我任何权威的信息,说明指定一个完成处理程序是否会阻止激发委托方法,但是这种关系有很多是相互排斥的。
如果您还没有,我想您应该添加–URLSession:task:didCompleteWithError:到您的代表。它可能捕获纯下载委托方法可能遗漏的问题。
发布于 2014-02-12 12:27:38
现在使用NSUrlRequest,委托将调用。希望这能起作用
- (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.
}https://stackoverflow.com/questions/21726270
复制相似问题