JTSImageViewController
JTSImageViewController copied to clipboard
Adding support for image downloading using other download sources
For my project, I would like to use the same image caching solution between this image viewer and every other uiimageview in the project. This update allow me to do so. I've added two more samples to the sample app using SDWebimage for custom image caching.
This looks really great! Welcome improvements. I have a few questions.
New Dependency?
So SDWebImage
has only been included for the sample app, right? It's not an external dependency?
Naming
I think the new methods would benefit from more self-explanatory names. Something more like this:
- (instancetype)initWithImageInfo:(JTSImageInfo *)imageInfo
mode:(JTSImageViewControllerMode)mode
backgroundStyle:(JTSImageViewControllerBackgroundStyle)backgroundStyle
customImageLoadingProgress:(NSProgress*)progress;
And this:
-(void)customImageLoadingDidFinish:(UIImage*)image;
NSProgress
Are there any potential conflicts when using an NSProgress to track progress? I don't understand from reading the docs how multiple unrelated NSProgress instances are resolved.
Dependancy
SDWebImage was intended as dependancy for sample app only of course as an example of why it is useful to have this custom progress initializer. However, I removed it entirely as I didn't want to add so many files to the repo and didn't want to create a podfile for just a sample app. Instead I modified the sample app to use NSProgress directly using NSURLSessionDownloadTask request and it's delegate methods to update progress.
Naming
I agree they needed to be more verbose and I commited your suggestions.
NSProgress
This object is simply used as a way to encapsulate the progression of a unit of work. It is used in AFNetworking which is what inspired this need. Read Matt Thompson's explanation
Thanks for the clarifications. I'm AFK at the moment, but I'm looking forward to trying this pull request out for myself. :-)
Adding a comment here.
Hi @jaredsinclair , are you still merging that?
I know this is old, but for those who are using JTSImageViewController and want to cache with SDWebImage, you can easily achieve it by using the JTSImageViewControllerDismissalDelegate to cache when the view dismisses.
This is what I am doing right now:
- In the tap action on the UIImageView, check if image is cached:
- (void)tapImageViewAction:(id)sender {
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageURL.absoluteString];
if (image) {
JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
imageInfo.image = image;
imageInfo.referenceRect = self.imageView.frame;
imageInfo.referenceView = self.imageView.superview;
JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
initWithImageInfo:imageInfo
mode:JTSImageViewControllerMode_Image
backgroundStyle:JTSImageViewControllerBackgroundOption_Scaled];
[imageViewer showFromViewController:self transition:JTSImageViewControllerTransition_FromOriginalPosition];
}
else {
JTSImageInfo *imageInfo = [[JTSImageInfo alloc] init];
imageInfo.imageURL = imageURL;
imageInfo.referenceRect = self.imageView.frame;
imageInfo.referenceView = self.imageView.superview;
JTSImageViewController *imageViewer = [[JTSImageViewController alloc]
initWithImageInfo:imageInfo
mode:JTSImageViewControllerMode_Image
backgroundStyle:JTSImageViewControllerBackgroundOption_Scaled];
imageViewer.dismissalDelegate = self;
[imageViewer showFromViewController:self transition:JTSImageViewControllerTransition_FromOriginalPosition];
}
}
If an image is returned, then you just pass it to JTSImageInfo and present the controller. If not, then you pass the URL and let JTSImageViewController handle the download.
- Do the following to cache the image when image viewer dismissed:
- (void)imageViewerDidDismiss:(JTSImageViewController *)imageViewer {
if (imageViewer.image) {
[[SDImageCache sharedImageCache] storeImage:imageViewer.image forKey:imageURL.absoluteString];
}
}
Hope this helps. I do think keeping JTSImageViewController as light as possible is better.
Is this PR still being considered for merging? I too am needing to download from a specialized source, and it would be nice to tap into the progress bar JTSImageViewController displays when it is in charge of downloading the image.