Browse files

Placeholder support

  • Loading branch information...
1 parent aa4fd90 commit 7b9240a16e4c97e2087fdc02d8f0416c72d19f18 Ryan Bruels committed May 24, 2012
View
3 MWPhotoBrowser/Classes/MWPhoto.h
@@ -18,17 +18,20 @@
@interface MWPhoto : NSObject <MWPhoto, SDWebImageManagerDelegate, SDWebImageDecoderDelegate>
// Properties
+@property (nonatomic, retain) UIImage *placeholderImage;
@property (nonatomic, retain) NSString *caption;
// Class
+ (MWPhoto *)photoWithImage:(UIImage *)image;
+ (MWPhoto *)photoWithFilePath:(NSString *)path;
+ (MWPhoto *)photoWithURL:(NSURL *)url;
++ (MWPhoto *)photoWithURL:(NSURL *)url placeholderImage:(UIImage *)image;
// Init
- (id)initWithImage:(UIImage *)image;
- (id)initWithFilePath:(NSString *)path;
- (id)initWithURL:(NSURL *)url;
+- (id)initWithURL:(NSURL *)url placeholderImage:(UIImage *)image;
@end
View
19 MWPhotoBrowser/Classes/MWPhoto.m
@@ -18,6 +18,7 @@ @interface MWPhoto () {
// Image
UIImage *_underlyingImage;
+ UIImage *_placeholderImage;
// Other
NSString *_caption;
@@ -39,6 +40,7 @@ @implementation MWPhoto
// Properties
@synthesize underlyingImage = _underlyingImage,
+placeholderImage = _placeholderImage,
caption = _caption;
#pragma mark Class Methods
@@ -55,6 +57,10 @@ + (MWPhoto *)photoWithURL:(NSURL *)url {
return [[[MWPhoto alloc] initWithURL:url] autorelease];
}
++ (MWPhoto *)photoWithURL:(NSURL *)url placeholderImage:(UIImage *)image {
+ return [[[MWPhoto alloc] initWithURL:url placeholderImage:image] autorelease];
+}
+
#pragma mark NSObject
- (id)initWithImage:(UIImage *)image {
@@ -78,17 +84,30 @@ - (id)initWithURL:(NSURL *)url {
return self;
}
+- (id)initWithURL:(NSURL *)url placeholderImage:(UIImage *)image {
+ if ((self = [super init])) {
+ _placeholderImage = [image copy];
+ _photoURL = [url copy];
+ }
+ return self;
+}
+
- (void)dealloc {
[_caption release];
[[SDWebImageManager sharedManager] cancelForDelegate:self];
[_photoPath release];
[_photoURL release];
+ [_placeholderImage release];
[_underlyingImage release];
[super dealloc];
}
#pragma mark MWPhoto Protocol Methods
+- (UIImage *)placeholderImage {
+ return _placeholderImage;
+}
+
- (UIImage *)underlyingImage {
return _underlyingImage;
}
View
2 MWPhotoBrowser/Classes/MWPhotoBrowser.h
@@ -33,6 +33,8 @@
// Properties
@property (nonatomic) BOOL displayActionButton;
+@property (nonatomic, retain, readonly) UIToolbar *toolbar;
+@property (nonatomic, readonly) NSUInteger currentPageIndex;
// Init
- (id)initWithPhotos:(NSArray *)photosArray __attribute__((deprecated)); // Depreciated
View
6 MWPhotoBrowser/Classes/MWPhotoBrowser.m
@@ -139,6 +139,8 @@ @implementation MWPhotoBrowser
@synthesize displayActionButton = _displayActionButton, actionsSheet = _actionsSheet;
@synthesize progressHUD = _progressHUD;
@synthesize previousViewControllerBackButton = _previousViewControllerBackButton;
+@synthesize toolbar=_toolbar;
+@synthesize currentPageIndex=_currentPageIndex;
#pragma mark - NSObject
@@ -591,6 +593,10 @@ - (UIImage *)imageForPhoto:(id<MWPhoto>)photo {
return [photo underlyingImage];
} else {
[photo loadUnderlyingImageAndNotify];
+ if([photo placeholderImage])
+ {
+ return [photo placeholderImage];
+ }
}
}
return nil;
View
3 MWPhotoBrowser/Classes/MWPhotoProtocol.h
@@ -32,6 +32,9 @@
// methods returns nil.
- (UIImage *)underlyingImage;
+// Return placeholder UIImage to be displayed
+- (UIImage *)placeholderImage;
+
// Called when the browser has determined the underlying images is not
// already loaded into memory but needs it.
// You must load the image asyncronously (and decompress it for better performance).
View
23 MWPhotoBrowser/Classes/MWZoomingScrollView.m
@@ -44,7 +44,7 @@ - (id)initWithPhotoBrowser:(MWPhotoBrowser *)browser {
// Image view
_photoImageView = [[MWTapDetectingImageView alloc] initWithFrame:CGRectZero];
_photoImageView.tapDelegate = self;
- _photoImageView.contentMode = UIViewContentModeCenter;
+ _photoImageView.contentMode = UIViewContentModeScaleAspectFit;
_photoImageView.backgroundColor = [UIColor blackColor];
[self addSubview:_photoImageView];
@@ -94,7 +94,7 @@ - (void)prepareForReuse {
// Get and display image
- (void)displayImage {
- if (_photo && _photoImageView.image == nil) {
+ if (_photo) {
// Reset
self.maximumZoomScale = 1;
@@ -107,8 +107,15 @@ - (void)displayImage {
if (img) {
// Hide spinner
- [_spinner stopAnimating];
-
+ if(img == [_photo placeholderImage])
+ {
+ [_spinner startAnimating];
+ }
+ else
+ {
+ [_spinner stopAnimating];
+ }
+
// Set image
_photoImageView.image = img;
_photoImageView.hidden = NO;
@@ -117,7 +124,7 @@ - (void)displayImage {
CGRect photoImageViewFrame;
photoImageViewFrame.origin = CGPointZero;
photoImageViewFrame.size = img.size;
- _photoImageView.frame = photoImageViewFrame;
+ _photoImageView.frame = self.bounds;
self.contentSize = photoImageViewFrame.size;
// Set zoom to minimum zoom
@@ -151,6 +158,12 @@ - (void)setMaxMinZoomScalesForCurrentBounds {
// Bail
if (_photoImageView.image == nil) return;
+ // Bail if we're showing the placeholder
+ if(_photoImageView.image == [self.photo placeholderImage])
+ {
+ return;
+ }
+
// Sizes
CGSize boundsSize = self.bounds.size;
CGSize imageSize = _photoImageView.frame.size;

0 comments on commit 7b9240a

Please sign in to comment.