-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathBFRBackLoadedImageSource.m
65 lines (50 loc) · 2.29 KB
/
BFRBackLoadedImageSource.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// BFRBackLoadedImageSource.m
// BFRImageViewer
//
// Created by Jordan Morgan on 4/6/17.
// Copyright © 2017 Andrew Yates. All rights reserved.
//
#import "BFRBackLoadedImageSource.h"
#import "BFRImageViewerConstants.h"
#import <UIKit/UIKit.h>
#import <PINRemoteImage/PINRemoteImage.h>
#import <PINRemoteImage/PINImageView+PINRemoteImage.h>
@interface BFRBackLoadedImageSource()
/*! The high fidelity image that will be loaded in the background and then shown once it's downloaded. */
@property (strong, nonatomic, nonnull) NSURL *url;
/*! The image that will show initially. */
@property (strong, nonatomic, readwrite, nonnull) UIImage *image;
@end
@implementation BFRBackLoadedImageSource
#pragma mark - Initializers
- (instancetype)initWithInitialImage:(UIImage *)image hiResURL:(NSURL *)url {
self = [super init];
if (self) {
self.image = image;
self.url = url;
[self loadHighFidelityImage];
}
return self;
}
#pragma mark - Backloading
- (void)loadHighFidelityImage {
[[PINRemoteImageManager sharedImageManager] downloadImageWithURL:self.url options:PINRemoteImageManagerDownloadOptionsNone progressDownload:nil completion:^(PINRemoteImageManagerResult * _Nonnull result) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self.onCompletion != nil) {
if (result.image) {
self.onCompletion(result.image, nil);
} else {
NSLog(@"BFRImageViewer: Unable to load high resolution photo via backloading.");
NSError *downloadError = [NSError errorWithDomain:HI_RES_IMG_ERROR_DOMAIN
code:HI_RES_IMG_ERROR_CODE
userInfo:@{NSLocalizedFailureReasonErrorKey:[NSString stringWithFormat:@"Failed to download an image for high resolution url %@", self.url.absoluteString]}];
self.onCompletion(nil, downloadError);
}
}
id returnResult = result.alternativeRepresentation ? result.alternativeRepresentation : result.image;
[[NSNotificationCenter defaultCenter] postNotificationName:NOTE_HI_RES_IMG_DOWNLOADED object:returnResult];
});
}];
}
@end