Permalink
Browse files

[CHANGE] Added imageKeyPath property to allow non-NSImage content

  • Loading branch information...
mattball committed Mar 29, 2009
1 parent 29a770b commit 4292f249a7697071eaf9cdc52a9527dcec98fc68
Showing with 48 additions and 15 deletions.
  1. +8 −0 MBCoverFlowView.h
  2. +40 −15 MBCoverFlowView.m
View
@@ -51,6 +51,8 @@
BOOL _showsScrollbar;
BOOL _autoresizesItems;
+
+ NSString *_imageKeyPath;
}
/**
@@ -62,6 +64,12 @@
*/
@property (nonatomic, copy) NSArray *content;
+/**
+ * @brief The key path which returns the image for an item
+ * in the receiver's \c content array.
+ */
+@property (nonatomic, copy) NSString *imageKeyPath;
+
/**
* @name Setting Display Attributes
*/
View
@@ -62,14 +62,15 @@ @interface MBCoverFlowView ()
- (float)_positionOfSelectedItem;
- (CALayer *)_newLayer;
- (void)_scrollerChange:(MBCoverFlowScroller *)scroller;
+- (void)_refreshImagesWithOldContent:(NSArray *)oldContent;
@end
@implementation MBCoverFlowView
@synthesize accessoryController=_accessoryController, selectionIndex=_selectionIndex,
itemSize=_itemSize, content=_content, showsScrollbar=_showsScrollbar,
- autoresizesItems=_autoresizesItems;
+ autoresizesItems=_autoresizesItems, imageKeyPath=_imageKeyPath;
#pragma mark -
#pragma mark Life Cycle
@@ -235,6 +236,8 @@ - (void)dealloc
[_scrollLayer release];
[_containerLayer release];
self.accessoryController = nil;
+ self.content = nil;
+ self.imageKeyPath = nil;
CGImageRelease(_shadowImage);
[super dealloc];
}
@@ -339,29 +342,19 @@ - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
- (void)setContent:(NSArray *)newContents
{
+ NSArray *oldContent = [self.content retain];
+
if (_content) {
[_content release];
_content = nil;
}
if (newContents != nil) {
_content = [newContents copy];
- for (NSImage *image in self.content) {
- CALayer *layer = [self _newLayer];
- CALayer *imageLayer = [[layer sublayers] objectAtIndex:0];
- CALayer *reflectionLayer = [[imageLayer sublayers] objectAtIndex:0];
-
- CGImageRef imageRef = [image imageRef];
-
- imageLayer.contents = (id)imageRef;
- reflectionLayer.contents = (id)imageRef;
- imageLayer.backgroundColor = NULL;
- reflectionLayer.backgroundColor = NULL;
- }
}
- [_scroller setNumberOfIncrements:([self.content count]-1)];
- self.selectionIndex = self.selectionIndex;
+ [self _refreshImagesWithOldContent:oldContent];
+ [oldContent release];
}
#pragma mark Setting Display Attributes
@@ -608,6 +601,38 @@ - (void)_scrollerChange:(MBCoverFlowScroller *)sender
}
}
+- (void)_refreshImagesWithOldContent:(NSArray *)oldContent
+{
+ for (NSObject *object in self.content) {
+ CALayer *layer = [self _newLayer];
+ CALayer *imageLayer = [[layer sublayers] objectAtIndex:0];
+ CALayer *reflectionLayer = [[imageLayer sublayers] objectAtIndex:0];
+
+ @try {
+ NSImage *image;
+
+ if (self.imageKeyPath != nil) {
+ image = [object valueForKeyPath:self.imageKeyPath];
+ } else if ([object isKindOfClass:[NSImage class]]) {
+ image = (NSImage *)object;
+ }
+
+ CGImageRef imageRef = [image imageRef];
+
+ imageLayer.contents = (id)imageRef;
+ reflectionLayer.contents = (id)imageRef;
+ imageLayer.backgroundColor = NULL;
+ reflectionLayer.backgroundColor = NULL;
+ } @catch (NSException *e) {
+ // If the key path isn't valid, move to the next item
+ continue;
+ }
+ }
+
+ [_scroller setNumberOfIncrements:([self.content count]-1)];
+ self.selectionIndex = self.selectionIndex;
+}
+
#pragma mark -
#pragma mark Protocol Methods

0 comments on commit 4292f24

Please sign in to comment.