Skip to content

Commit

Permalink
Photo download progress now shown in circular indicator.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwaterfall committed Oct 2, 2013
1 parent 17e65c0 commit 47d9ac3
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Demo/PhotoBrowserDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
0E17D70117FAE1A80094ABF6 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 0E17D70017FAE1A80094ABF6 /* Default-568h@2x.png */; };
0E76FFEF1573FE1A000EE5CA /* MWPhotoBrowser.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 0E76FFEE1573FE1A000EE5CA /* MWPhotoBrowser.bundle */; };
0E76FFF21573FE25000EE5CA /* libMWPhotoBrowser.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0E76FFED1573FE14000EE5CA /* libMWPhotoBrowser.a */; };
0E8515C617FC781600FE1EF3 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0E8515C517FC781600FE1EF3 /* QuartzCore.framework */; };
0E85B24C14C6F40500EA35BF /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 0E85B24E14C6F40500EA35BF /* Localizable.strings */; };
0EA380351573EA53003F4582 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0EA380341573EA53003F4582 /* ImageIO.framework */; };
4C6F979214AF734900F8389A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C6F979114AF734900F8389A /* UIKit.framework */; };
Expand Down Expand Up @@ -50,6 +51,7 @@
0E17D70017FAE1A80094ABF6 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
0E76FFE51573FE13000EE5CA /* MWPhotoBrowser.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = MWPhotoBrowser.xcodeproj; path = ../../MWPhotoBrowser/MWPhotoBrowser.xcodeproj; sourceTree = "<group>"; };
0E76FFEE1573FE1A000EE5CA /* MWPhotoBrowser.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = MWPhotoBrowser.bundle; path = ../../MWPhotoBrowser/MWPhotoBrowser.bundle; sourceTree = "<group>"; };
0E8515C517FC781600FE1EF3 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
0E85B24D14C6F40500EA35BF /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
0EA380341573EA53003F4582 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; };
4C6F978D14AF734900F8389A /* PhotoBrowserDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PhotoBrowserDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -79,6 +81,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
0E8515C617FC781600FE1EF3 /* QuartzCore.framework in Frameworks */,
0E76FFF21573FE25000EE5CA /* libMWPhotoBrowser.a in Frameworks */,
0EA380351573EA53003F4582 /* ImageIO.framework in Frameworks */,
4C6F97C914AF750400F8389A /* MessageUI.framework in Frameworks */,
Expand Down Expand Up @@ -120,6 +123,7 @@
4C6F979014AF734900F8389A /* Frameworks */ = {
isa = PBXGroup;
children = (
0E8515C517FC781600FE1EF3 /* QuartzCore.framework */,
0EA380341573EA53003F4582 /* ImageIO.framework */,
4C6F97C814AF750400F8389A /* MessageUI.framework */,
4C6F979114AF734900F8389A /* UIKit.framework */,
Expand Down
8 changes: 7 additions & 1 deletion MWPhotoBrowser/Classes/MWPhoto.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ - (void)loadUnderlyingImageAndNotify {
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:_photoURL
options:0
progress:nil
progress:^(NSUInteger receivedSize, long long expectedSize) {
float progress = receivedSize / (float)expectedSize;
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:progress], @"progress",
self, @"photo", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:MWPHOTO_PROGRESS_NOTIFICATION object:dict];
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
if (error) {
MWLog(@"SDWebImage failed to download image: %@", error);
Expand Down
4 changes: 2 additions & 2 deletions MWPhotoBrowser/Classes/MWPhotoProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#import <Foundation/Foundation.h>

// Name of notification used when a photo has completed loading process
// Used to notify browser display the image
// Notifications
#define MWPHOTO_LOADING_DID_END_NOTIFICATION @"MWPHOTO_LOADING_DID_END_NOTIFICATION"
#define MWPHOTO_PROGRESS_NOTIFICATION @"MWPHOTO_PROGRESS_NOTIFICATION"

// If you wish to use your own data models for photo then they must conform
// to this protocol. See instructions for details on each method.
Expand Down
67 changes: 53 additions & 14 deletions MWPhotoBrowser/Classes/MWZoomingScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "MWZoomingScrollView.h"
#import "MWPhotoBrowser.h"
#import "MWPhoto.h"
#import "DACircularProgressView.h"

// Declare private methods of browser
@interface MWPhotoBrowser ()
Expand All @@ -22,7 +23,7 @@ @interface MWZoomingScrollView () {

MWTapDetectingView *_tapView; // for background taps
MWTapDetectingImageView *_photoImageView;
UIActivityIndicatorView *_spinner;
DACircularProgressView *_loadingIndicator;

}

Expand Down Expand Up @@ -55,13 +56,25 @@ - (id)initWithPhotoBrowser:(MWPhotoBrowser *)browser {
_photoImageView.backgroundColor = [UIColor blackColor];
[self addSubview:_photoImageView];

// Spinner
_spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
_spinner.hidesWhenStopped = YES;
_spinner.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin |
// Loading indicator
_loadingIndicator = [[DACircularProgressView alloc] initWithFrame:CGRectMake(140.0f, 30.0f, 40.0f, 40.0f)];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
_loadingIndicator.thicknessRatio = 0.1;
_loadingIndicator.roundedCorners = NO;
} else {
_loadingIndicator.thicknessRatio = 0.2;
_loadingIndicator.roundedCorners = YES;
}
_loadingIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
[self addSubview:_spinner];

[self addSubview:_loadingIndicator];

// Listen progress notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setProgressFromNotification:)
name:MWPHOTO_PROGRESS_NOTIFICATION
object:nil];

// Setup
self.backgroundColor = [UIColor blackColor];
self.delegate = self;
Expand All @@ -74,6 +87,10 @@ - (id)initWithPhotoBrowser:(MWPhotoBrowser *)browser {
return self;
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setPhoto:(id<MWPhoto>)photo {
_photoImageView.image = nil; // Release image
if (_photo != photo) {
Expand Down Expand Up @@ -104,8 +121,8 @@ - (void)displayImage {
UIImage *img = [self.photoBrowser imageForPhoto:_photo];
if (img) {

// Hide spinner
[_spinner stopAnimating];
// Hide indicator
[self hideLoadingIndicator];

// Set image
_photoImageView.image = img;
Expand All @@ -125,7 +142,7 @@ - (void)displayImage {

// Hide image view
_photoImageView.hidden = YES;
[_spinner startAnimating];
[self showLoadingIndicator];

}
[self setNeedsLayout];
Expand All @@ -134,7 +151,28 @@ - (void)displayImage {

// Image failed so just show black!
- (void)displayImageFailure {
[_spinner stopAnimating];
[self hideLoadingIndicator];
}

#pragma mark - Loading Progress

- (void)setProgressFromNotification:(NSNotification *)notification {
NSDictionary *dict = [notification object];
MWPhoto *photoWithProgress = (MWPhoto *)[dict objectForKey:@"photo"];
if (photoWithProgress == self.photo) {
float progress = [[dict valueForKey:@"progress"] floatValue];
NSLog(@"Progress: %f", progress);
_loadingIndicator.progress = MAX(MIN(1, progress), 0);
}
}

- (void)hideLoadingIndicator {
_loadingIndicator.hidden = YES;
}

- (void)showLoadingIndicator {
_loadingIndicator.progress = 0;
_loadingIndicator.hidden = NO;
}

#pragma mark - Setup
Expand Down Expand Up @@ -209,9 +247,10 @@ - (void)layoutSubviews {
// Update tap view frame
_tapView.frame = self.bounds;

// Spinner
if (!_spinner.hidden) _spinner.center = CGPointMake(floorf(self.bounds.size.width/2.0),
floorf(self.bounds.size.height/2.0));
// Indicator
if (!_loadingIndicator.hidden)
_loadingIndicator.center = CGPointMake(floorf(self.bounds.size.width/2.0),
floorf(self.bounds.size.height/2.0));
// Super
[super layoutSubviews];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// DACircularProgressView.h
// DACircularProgress
//
// Created by Daniel Amitay on 2/6/12.
// Copyright (c) 2012 Daniel Amitay. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DACircularProgressView : UIView

@property(nonatomic, strong) UIColor *trackTintColor UI_APPEARANCE_SELECTOR;
@property(nonatomic, strong) UIColor *progressTintColor UI_APPEARANCE_SELECTOR;
@property(nonatomic) NSInteger roundedCorners UI_APPEARANCE_SELECTOR; // Can not use BOOL with UI_APPEARANCE_SELECTOR :-(
@property(nonatomic) CGFloat thicknessRatio UI_APPEARANCE_SELECTOR;
@property(nonatomic) CGFloat progress;

@property(nonatomic) CGFloat indeterminateDuration UI_APPEARANCE_SELECTOR;
@property(nonatomic) NSInteger indeterminate UI_APPEARANCE_SELECTOR; // Can not use BOOL with UI_APPEARANCE_SELECTOR :-(

- (void)setProgress:(CGFloat)progress animated:(BOOL)animated;

@end
Loading

0 comments on commit 47d9ac3

Please sign in to comment.