2016-07-16 23 views
2

Verwenden Sie diesen Code zum Herunterladen von Bildern. Das Bild erscheint jedoch nicht auf dem Display.Wie Bild herunterladen?

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

ich meinen Code in meinem Xcode-Projekt aktualisieren, aber es auch nicht funktioniert ... Warum es Code nicht auch in meinem Xcode-Projekt funktioniert?

#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. 
} 

Warum funktioniert Code nicht auch in meinem Xcode-Projekt?

+0

Zunächst versuchen Sie, das gerade heruntergeladene Image 'UIImage' (das sich im Speicher befindet) zu laden, als ob es in Ihrem Bundle wäre. Zweitens versuchen Sie, das Bild zu laden, bevor es asynchron heruntergeladen wird ... – Putz1103

Antwort

0

Wenn Sie dies wollen arbeiten, wie der Code steht (nicht sicher, dass es wird), dann müssen Sie die Schritte in diesem Prozess ausgeführt:

//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]; 
0

Sie erstellen die erste Instanz eines Bildes durch Laden Sie es von Ihrem Bundle: Sie erstellen eine zweite Instanz eines Bildes mit dem heruntergeladenen Inhalt _downloadedImage = [UIImage imageNamed:@"Williams_River-27527.jpg"]; (hoffentlich gibt es eine)

Nachdem das Bild von Ihrer uRL herunterzuladen. Aber Ihre Schaltfläche verweist immer noch auf die erste Instanz (starke Referenz). Der Button kennt das neue Bild einfach nicht.

Also, wenn Sie wollen, um das angezeigte Bild haben Sie es wieder ändern setzen direkt nach weist es _downloadedImage: [_book3 setBackgroundImage: _downloadedImage forState:UIControlStateNormal];

Vielleicht müssen aktualisieren Sie die ui danach.