Guest User

Untitled

a guest
May 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #import "OMImageTableViewController.h"
  2. #import "OMImageTableViewCell.h"
  3. #import <AFNetworking.h>
  4. #import <AFNetworking/UIImageView+AFNetworking.h>
  5.  
  6. static NSString* const identifier = @"cellImage";
  7.  
  8. @interface OMImageTableViewController () <OMImageTableViewCellDelegate>
  9.  
  10. @property (strong, nonatomic) NSArray *arrayImages;
  11. @property (strong, nonatomic) NSURL *documentsDirectoryURL;
  12.  
  13. @end
  14.  
  15. @implementation OMImageTableViewController
  16.  
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19.  
  20. UIEdgeInsets insets = UIEdgeInsetsMake(10, 0, 0, 0);
  21. self.tableView.contentInset = insets;
  22.  
  23. _arrayImages = [NSArray array];
  24.  
  25. NSString *pathResource = [[NSBundle mainBundle] pathForResource:@"imagesList" ofType:@"plist"];
  26. NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:pathResource];
  27. NSString *image1 = [dictionary valueForKey:@"image1"];
  28. NSString *image2 = [dictionary valueForKey:@"image2"];
  29. NSString *image3 = [dictionary valueForKey:@"image3"];
  30. NSString *image4 = [dictionary valueForKey:@"image4"];
  31. NSString *image5 = [dictionary valueForKey:@"image5"];
  32. NSString *image6 = [dictionary valueForKey:@"image6"];
  33. NSString *image7 = [dictionary valueForKey:@"image7"];
  34. NSString *image8 = [dictionary valueForKey:@"image8"];
  35. NSString *image9 = [dictionary valueForKey:@"image9"];
  36. NSString *image10 = [dictionary valueForKey:@"image10"];
  37. NSString *image11 = [dictionary valueForKey:@"image11"];
  38. NSString *image12 = [dictionary valueForKey:@"image12"];
  39. NSString *image13 = [dictionary valueForKey:@"image13"];
  40. NSString *image14 = [dictionary valueForKey:@"image14"];
  41. NSString *image15 = [dictionary valueForKey:@"image15"];
  42. NSString *image16 = [dictionary valueForKey:@"image16"];
  43. NSString *image17 = [dictionary valueForKey:@"image17"];
  44. NSString *image18 = [dictionary valueForKey:@"image18"];
  45. NSString *image19 = [dictionary valueForKey:@"image19"];
  46. NSString *image20 = [dictionary valueForKey:@"image20"];
  47.  
  48. _arrayImages = @[image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12, image13, image14, image15, image16, image17, image18, image19, image20];
  49.  
  50. }
  51.  
  52. - (void)startDownload:(OMImageTableViewCell *)cell {
  53.  
  54. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  55. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  56.  
  57. NSURL *url = [NSURL URLWithString:[_arrayImages objectAtIndex:cell.download.tag]];
  58. NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
  59.  
  60. [cell.imagePic setImageWithURLRequest:request placeholderImage:cell.imagePic.image success:nil failure:nil];
  61.  
  62. NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
  63.  
  64. dispatch_async(dispatch_get_main_queue(), ^{
  65. [cell.progressDownload setProgress:downloadProgress.fractionCompleted];
  66. });
  67.  
  68. } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
  69.  
  70. _documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
  71. return [_documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
  72.  
  73. } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
  74.  
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. NSData *data = [NSData dataWithContentsOfURL:filePath];
  77. cell.imagePic.image = [UIImage imageWithData:data];
  78. });
  79.  
  80. NSLog(@"File downloaded to: %@", filePath);
  81. }];
  82. [downloadTask resume];
  83.  
  84. }
  85.  
  86. #pragma mark - UITableViewDataSource
  87.  
  88. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  89.  
  90. return _arrayImages.count;
  91.  
  92. }
  93.  
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  95.  
  96. OMImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  97.  
  98. if (!cell) {
  99. cell = [[OMImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  100. }
  101.  
  102. cell.download.tag = indexPath.row;
  103.  
  104. cell.delegate = self;
  105.  
  106. return cell;
  107.  
  108. }
  109.  
  110. @end
Add Comment
Please, Sign In to add comment