Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ profile
DerivedData

# Demo Images
FastImageCache/FastImageCacheDemo/Demo Images/*.jpg
FastImageCacheDemo/Demo Images/*.jpg
Carthage
68 changes: 36 additions & 32 deletions FastImageCache/FastImageCacheDemo/Classes/FICDPhotosTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ @interface FICDPhotosTableViewCell () <UIGestureRecognizerDelegate> {
NSArray *_photos;
NSString *_imageFormatName;

NSArray *_imageViews;
NSMutableArray *_imageViews;
UITapGestureRecognizer *_tapGestureRecognizer;
}

Expand All @@ -39,28 +39,42 @@ @implementation FICDPhotosTableViewCell
- (void)setPhotos:(NSArray *)photos {
if (photos != _photos) {
_photos = [photos copy];

for (NSInteger i = 0; i < [_imageViews count]; i++) {

// Either create the image views for this cell or clear them out if they already exist
if (_imageViews == nil) {
NSInteger photosPerRow = [[self class] photosPerRow];
_imageViews = [[NSMutableArray alloc] initWithCapacity:photosPerRow];

for (NSInteger i = 0; i < photosPerRow; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[_imageViews addObject:imageView];
}
} else {
for (UIImageView *imageView in _imageViews) {
[imageView setImage:nil];
[imageView removeFromSuperview];
}
}

NSInteger photosCount = [_photos count];
for (NSInteger i = 0; i < photosCount; i++) {
FICDPhoto *photo = [_photos objectAtIndex:i];
UIImageView *imageView = [_imageViews objectAtIndex:i];

if (i < [_photos count]) {
FICDPhoto *photo = [_photos objectAtIndex:i];

if (_usesImageTable) {
[[FICImageCache sharedImageCache] retrieveImageForEntity:photo withFormatName:_imageFormatName completionBlock:^(id<FICEntity> entity, NSString *formatName, UIImage *image) {
// This completion block may be called much later. We should check to make sure this cell hasn't been reused for different photos before displaying the image that has loaded.
if (photos == [self photos]) {
[imageView setImage:image];
}
}];
} else {
[imageView setImage:[photo thumbnailImage]];
}

if (_usesImageTable) {
[[FICImageCache sharedImageCache] retrieveImageForEntity:photo withFormatName:_imageFormatName completionBlock:^(id<FICEntity> entity, NSString *formatName, UIImage *image) {
// This completion block may be called much later. We should check to make sure this cell hasn't been reused for different photos before displaying the image that has loaded.
if (photos == [self photos]) {
[imageView setImage:image];
}
}];
} else {
// Last row might not be full
[imageView setImage:nil];
[imageView setImage:[photo thumbnailImage]];
}
}

[self setNeedsLayout];
}
}

Expand Down Expand Up @@ -103,18 +117,6 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
if (self != nil) {
_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapGestureRecognizerStateDidChange)];
[self addGestureRecognizer:_tapGestureRecognizer];

NSInteger photosPerRow = [[self class] photosPerRow];
NSMutableArray *imageViews = [[NSMutableArray alloc] initWithCapacity:photosPerRow];

for (NSInteger i = 0; i < photosPerRow; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageViews addObject:imageView];
[self.contentView addSubview:imageView];
}

_imageViews = [imageViews copy];
}

return self;
Expand All @@ -137,12 +139,14 @@ - (void)layoutSubviews {
CGFloat outerPadding = [[self class] outerPadding];

CGRect imageViewFrame = CGRectMake(outerPadding, outerPadding, FICDPhotoSquareImageSize.width, FICDPhotoSquareImageSize.height);


UIView *contentView = [self contentView];
NSInteger count = [_photos count];

for (NSInteger i = 0; i < count; i++) {
UIImageView *imageView = [_imageViews objectAtIndex:i];
[imageView setFrame:imageViewFrame];
[contentView addSubview:imageView];

imageViewFrame.origin.x += imageViewFrame.size.width + innerPadding;
}
Expand Down
11 changes: 7 additions & 4 deletions FastImageCache/FastImageCacheDemo/Classes/FICDViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ - (void)loadView {
[_tableView setDelegate:self];
[_tableView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[_tableView registerClass:[FICDPhotosTableViewCell class] forCellReuseIdentifier:[FICDPhotosTableViewCell reuseIdentifier]];

CGFloat tableViewCellOuterPadding = [FICDPhotosTableViewCell outerPadding];
[_tableView setContentInset:UIEdgeInsetsMake(0, 0, tableViewCellOuterPadding, 0)];
Expand Down Expand Up @@ -462,9 +461,13 @@ - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)
- (UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath*)indexPath {
NSString *reuseIdentifier = [FICDPhotosTableViewCell reuseIdentifier];

FICDPhotosTableViewCell *tableViewCell = (FICDPhotosTableViewCell *)[table dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
tableViewCell.selectionStyle = UITableViewCellSeparatorStyleNone;

FICDPhotosTableViewCell *tableViewCell = (FICDPhotosTableViewCell *)[table dequeueReusableCellWithIdentifier:reuseIdentifier];
if (tableViewCell == nil) {
tableViewCell = [[FICDPhotosTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
[tableViewCell setBackgroundColor:[table backgroundColor]];
[tableViewCell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

[tableViewCell setDelegate:self];
[tableViewCell setImageFormatName:_imageFormatName];

Expand Down