Skip to content

Commit c83dc55

Browse files
committed
Check if cell has been recycled before setting attachment image
* Ensure that when the FIC completion block is called that the cell has not been recycled and used for another attachment. fixes #43
1 parent d3c71d0 commit c83dc55

File tree

7 files changed

+107
-116
lines changed

7 files changed

+107
-116
lines changed

MAGE.xcodeproj/project.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@
10211021
ORGANIZATIONNAME = "National Geospatial Intelligence Agency";
10221022
TargetAttributes = {
10231023
F7A94D6518AD9CB000CB9EE0 = {
1024-
DevelopmentTeam = A8EK924J35;
1024+
DevelopmentTeam = ZL8G5D9G2H;
10251025
};
10261026
};
10271027
};
@@ -1382,7 +1382,7 @@
13821382
INFOPLIST_FILE = "Mage/MAGE-Info.plist";
13831383
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
13841384
OTHER_LDFLAGS = "$(inherited)";
1385-
PRODUCT_BUNDLE_IDENTIFIER = mil.nga.giat.MAGE;
1385+
PRODUCT_BUNDLE_IDENTIFIER = mil.nga.mage;
13861386
PRODUCT_NAME = MAGE;
13871387
PROVISIONING_PROFILE = "";
13881388
WRAPPER_EXTENSION = app;
@@ -1404,7 +1404,7 @@
14041404
INFOPLIST_FILE = "Mage/MAGE-Info.plist";
14051405
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
14061406
OTHER_LDFLAGS = "$(inherited)";
1407-
PRODUCT_BUNDLE_IDENTIFIER = mil.nga.giat.MAGE;
1407+
PRODUCT_BUNDLE_IDENTIFIER = mil.nga.mage;
14081408
PRODUCT_NAME = MAGE;
14091409
PROVISIONING_PROFILE = "";
14101410
WRAPPER_EXTENSION = app;

Mage/AttachmentCell.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66

77
#import <UIKit/UIKit.h>
88

9+
@class Attachment;
10+
911
@interface AttachmentCell : UICollectionViewCell
1012

1113
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
14+
@property (strong, nonatomic) Attachment *attachment;
15+
16+
-(void) setImageForAttachament:(Attachment *) attachment;
1217

1318
@end

Mage/AttachmentCell.m

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55
//
66

77
#import "AttachmentCell.h"
8+
#import "FICImageCache.h"
9+
#import "Attachment+FICAttachment.h"
10+
#import "AppDelegate.h"
811

912
@implementation AttachmentCell
1013

11-
- (id)initWithFrame:(CGRect)frame
12-
{
13-
self = [super initWithFrame:frame];
14-
if (self) {
15-
// Initialization code
16-
}
17-
return self;
14+
- (void)prepareForReuse {
15+
self.imageView.image = nil;
1816
}
1917

20-
/*
21-
// Only override drawRect: if you perform custom drawing.
22-
// An empty implementation adversely affects performance during animation.
23-
- (void)drawRect:(CGRect)rect
24-
{
25-
// Drawing code
18+
-(void) setImageForAttachament:(Attachment *) attachment {
19+
self.attachment = attachment;
20+
21+
self.imageView.image = [UIImage imageNamed:@"download"];
22+
23+
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
24+
__weak typeof(self) weakSelf = self;
25+
[delegate.imageCache retrieveImageForEntity:attachment withFormatName:AttachmentMediumSquare completionBlock:^(id<FICEntity> entity, NSString *formatName, UIImage *image) {
26+
// This completion block may be called much later, check to make sure this cell hasn't been reused for a different attachment before displaying the image that has loaded.
27+
if (attachment == [self attachment]) {
28+
weakSelf.imageView.image = image;
29+
[weakSelf.imageView.layer addAnimation:[CATransition animation] forKey:kCATransition];
30+
weakSelf.imageView.layer.cornerRadius = 5;
31+
weakSelf.imageView.clipsToBounds = YES;
32+
}
33+
}];
2634
}
27-
*/
2835

2936
@end

Mage/AttachmentCollectionDataStore.m

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,26 @@
1212
@implementation AttachmentCollectionDataStore
1313

1414
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
15-
NSMutableArray *allAttachments = [NSMutableArray arrayWithArray:[_observation.attachments allObjects]];
16-
[allAttachments addObjectsFromArray:_observation.transientAttachments];
15+
NSMutableArray *allAttachments = [NSMutableArray arrayWithArray:[self.observation.attachments allObjects]];
16+
[allAttachments addObjectsFromArray:self.observation.transientAttachments];
1717

18-
AttachmentCell *cell = [_attachmentCollection dequeueReusableCellWithReuseIdentifier:@"AttachmentCell" forIndexPath:indexPath];
19-
Attachment *attachment = [allAttachments objectAtIndex:[indexPath indexAtPosition:[indexPath length]-1]];
20-
21-
FICImageCacheCompletionBlock completionBlock = ^(id <FICEntity> entity, NSString *formatName, UIImage *image) {
22-
if (!image) return;
23-
24-
cell.imageView.image = image;
25-
[cell.imageView.layer addAnimation:[CATransition animation] forKey:kCATransition];
26-
cell.imageView.layer.cornerRadius = 5;
27-
cell.imageView.clipsToBounds = YES;
28-
};
29-
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
30-
31-
BOOL imageExists = [delegate.imageCache retrieveImageForEntity:attachment withFormatName:AttachmentMediumSquare completionBlock:completionBlock];
32-
33-
if (imageExists == NO) {
34-
cell.imageView.image = [UIImage imageNamed:@"download"];
35-
}
18+
AttachmentCell *cell = [self.attachmentCollection dequeueReusableCellWithReuseIdentifier:@"AttachmentCell" forIndexPath:indexPath];
19+
Attachment *attachment = [allAttachments objectAtIndex:[indexPath row]];
20+
[cell setImageForAttachament:attachment];
21+
3622
return cell;
3723
}
3824

3925
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
40-
return _observation.attachments.count + _observation.transientAttachments.count;
26+
return self.observation.attachments.count + self.observation.transientAttachments.count;
4127
}
4228

4329
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
44-
NSMutableArray *allAttachments = [NSMutableArray arrayWithArray:[_observation.attachments allObjects]];
45-
[allAttachments addObjectsFromArray:_observation.transientAttachments];
46-
47-
Attachment *attachment = [allAttachments objectAtIndex:[indexPath indexAtPosition:[indexPath length]-1]];
48-
NSLog(@"clicked attachment %@", attachment.url);
30+
NSMutableArray *allAttachments = [NSMutableArray arrayWithArray:[self.observation.attachments allObjects]];
31+
[allAttachments addObjectsFromArray:self.observation.transientAttachments];
4932

5033
if (self.attachmentSelectionDelegate) {
34+
Attachment *attachment = [allAttachments objectAtIndex:[indexPath row]];
5135
[self.attachmentSelectionDelegate selectedAttachment:attachment];
5236
}
5337
}

0 commit comments

Comments
 (0)