使用此代码下载图像。但是图像没有显示。

  NSURL *url = [NSURL URLWithString:
 @"http://upload.wikimedia.org/wikipedia/commons/7/7f/Williams_River-27527.jpg"];

NSURLSessionDownloadTask *downloadPhotoTask = [[NSURLSession sharedSession]
 downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
  _downloadedImage = [UIImage imageWithData:
    [NSData dataWithContentsOfURL:location]];
}];

// 4
[downloadPhotoTask resume];

_downloadedImage = [UIImage imageNamed:@"Williams_River-27527.jpg"];
    [_book3 setBackgroundImage: _downloadedImage forState:UIControlStateNormal];
    [_scroller addSubview:_book3];

UPD

我在xcode项目中更新了代码,但它也无法正常工作...为什么在我的Xcode项目中它也无法正常工作?
#import "ViewController.h"

@interface ViewController () <NSURLSessionDelegate, NSURLSessionDownloadDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:@"http://cdn.tutsplus.com/mobile/uploads/2013/12/sample.jpg"]];
    [downloadTask resume];

}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    NSData *data = [NSData dataWithContentsOfURL:location];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.progressView setHidden:YES];
        [self.imageView setImage:[UIImage imageWithData:data]];
    });
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {

}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
    float progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.progressView setProgress:progress];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

为什么它的代码在我的Xcode项目中也不起作用?

最佳答案

如果您希望它按照代码的原样运行(不确定是否会这样做),则需要运行此过程中的步骤:

//1
NSURL *url = [NSURL URLWithString:
  @"http://upload.wikimedia.org/wikipedia/commons/7/7f/Williams_River-27527.jpg"];

// 2
NSURLSessionDownloadTask *downloadPhotoTask = [[NSURLSession sharedSession]
 downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    // 3
    _downloadedImage = [UIImage imageWithData:
    [NSData dataWithContentsOfURL:location]];

    //5
    dispatch_async(dispatch_get_main_queue(), ^{
        [_book3 setBackgroundImage: _downloadedImage forState:UIControlStateNormal];
        [_scroller addSubview:_book3];
    });
}];

// 4
[downloadPhotoTask resume];

关于ios - 如何下载图片?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38411612/

10-09 13:01