2016-05-27 179 views
1

我使用afnetworking下載了圖片並保存了名稱[response suggestedFilename];我無法從路徑中獲取圖像。如何從路徑中獲取圖片

這裏是下載方法:

- (void)startDownload:(OMImageTableViewCell *)cell { 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

NSURL *url = [NSURL URLWithString:[_arrayImages objectAtIndex:cell.download.tag]]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60]; 

[cell.imagePic setImageWithURLRequest:request placeholderImage:cell.imagePic.image success:nil failure:nil]; 

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [cell.progressDownload setProgress:downloadProgress.fractionCompleted]; 
    }); 

} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 

    _documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 
    return [_documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; 

} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
    NSData *data = [NSData dataWithContentsOfURL:filePath]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     cell.imagePic.image = [UIImage imageWithData:data]; 
    }); 

    NSLog(@"File downloaded to: %@", filePath); 
}]; 
[downloadTask resume]; 

} 
+0

你如何試圖讓圖像?粘貼一些代碼。 – kelin

回答

0

試試這個:

NSFileManager *fileManager = [NSFileManager defaultManager]; 
BOOL isFileExist = [fileManager fileExistsAtPath: filePath]; 
UIImage *image; 
if (isFileExist) { 
    image = [[UIImage alloc] initWithContentsOfFile:filePath]; 
} 
else { 
    // do something. 
} 
+0

它沒有幫助。我想把它們放在uitableviewcell中以反映只下載的圖片。 –

+0

也許你需要這個框架:https://github.com/rs/SDWebImage。只需#import UIImageView + WebCache.h頭文件,並從tableView:cellForRowAtIndexPath:UITableViewDataSource方法中調用sd_setImageWithURL:placeholderImage:方法。從異步下載到緩存管理,一切都將爲您處理。 – Patrick

+0

謝謝!)這是工作) –

相關問題