Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "OMImageTableViewController.h"
- #import "OMImageTableViewCell.h"
- #import <AFNetworking.h>
- #import <AFNetworking/UIImageView+AFNetworking.h>
- static NSString* const identifier = @"cellImage";
- @interface OMImageTableViewController () <OMImageTableViewCellDelegate>
- @property (strong, nonatomic) NSArray *arrayImages;
- @property (strong, nonatomic) NSURL *documentsDirectoryURL;
- @end
- @implementation OMImageTableViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- UIEdgeInsets insets = UIEdgeInsetsMake(10, 0, 0, 0);
- self.tableView.contentInset = insets;
- _arrayImages = [NSArray array];
- NSString *pathResource = [[NSBundle mainBundle] pathForResource:@"imagesList" ofType:@"plist"];
- NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:pathResource];
- NSString *image1 = [dictionary valueForKey:@"image1"];
- NSString *image2 = [dictionary valueForKey:@"image2"];
- NSString *image3 = [dictionary valueForKey:@"image3"];
- NSString *image4 = [dictionary valueForKey:@"image4"];
- NSString *image5 = [dictionary valueForKey:@"image5"];
- NSString *image6 = [dictionary valueForKey:@"image6"];
- NSString *image7 = [dictionary valueForKey:@"image7"];
- NSString *image8 = [dictionary valueForKey:@"image8"];
- NSString *image9 = [dictionary valueForKey:@"image9"];
- NSString *image10 = [dictionary valueForKey:@"image10"];
- NSString *image11 = [dictionary valueForKey:@"image11"];
- NSString *image12 = [dictionary valueForKey:@"image12"];
- NSString *image13 = [dictionary valueForKey:@"image13"];
- NSString *image14 = [dictionary valueForKey:@"image14"];
- NSString *image15 = [dictionary valueForKey:@"image15"];
- NSString *image16 = [dictionary valueForKey:@"image16"];
- NSString *image17 = [dictionary valueForKey:@"image17"];
- NSString *image18 = [dictionary valueForKey:@"image18"];
- NSString *image19 = [dictionary valueForKey:@"image19"];
- NSString *image20 = [dictionary valueForKey:@"image20"];
- _arrayImages = @[image1, image2, image3, image4, image5, image6, image7, image8, image9, image10, image11, image12, image13, image14, image15, image16, image17, image18, image19, image20];
- }
- - (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) {
- dispatch_async(dispatch_get_main_queue(), ^{
- NSData *data = [NSData dataWithContentsOfURL:filePath];
- cell.imagePic.image = [UIImage imageWithData:data];
- });
- NSLog(@"File downloaded to: %@", filePath);
- }];
- [downloadTask resume];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _arrayImages.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- OMImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
- if (!cell) {
- cell = [[OMImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
- }
- cell.download.tag = indexPath.row;
- cell.delegate = self;
- return cell;
- }
- @end
Add Comment
Please, Sign In to add comment