From 9fd4ead1550c60c2392b1541abcca5b9d588d876 Mon Sep 17 00:00:00 2001 From: moritz haarmann Date: Mon, 28 Apr 2014 14:50:24 +0200 Subject: [PATCH] + Improved Performance of Attachment Viewer. --- Classes/BITAttachmentGalleryViewController.m | 81 ++++++++++++-------- Classes/BITImageAnnotationViewController.m | 3 +- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/Classes/BITAttachmentGalleryViewController.m b/Classes/BITAttachmentGalleryViewController.m index bcee160260..0a10dbebaa 100644 --- a/Classes/BITAttachmentGalleryViewController.m +++ b/Classes/BITAttachmentGalleryViewController.m @@ -19,19 +19,13 @@ @property (nonatomic) NSInteger currentIndex; @property (nonatomic) NSInteger loadedImageIndex; @property (nonatomic, strong) UITapGestureRecognizer *tapognizer; +@property (nonatomic, strong) NSMutableDictionary *images; @end @implementation BITAttachmentGalleryViewController -- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil -{ - self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; - if (self) { - // Custom initialization - } - return self; -} +#pragma mark - UIViewController - (void)viewDidLoad { @@ -58,6 +52,7 @@ self.tapognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)]; [self.view addGestureRecognizer:self.tapognizer]; } + -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; @@ -70,21 +65,34 @@ if (indexOfSelectedAttachment != NSNotFound){ self.currentIndex = indexOfSelectedAttachment; self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * self.currentIndex, 0); - } } + self.images = [NSMutableDictionary new]; [self layoutViews]; - - } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; - - - } + +-(void)viewWillDisappear:(BOOL)animated { + [super viewWillDisappear:animated]; + [self.images removeAllObjects]; +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + [self.images removeAllObjects]; +} + +- (BOOL)prefersStatusBarHidden { + return self.navigationController.navigationBarHidden; +} + +#pragma mark - Scroll View Content/Layout + - (void)setupScrollView { self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; self.view.autoresizesSubviews = NO; @@ -122,18 +130,6 @@ self.extractedAttachments = extractedOnes; } -- (void)didReceiveMemoryWarning -{ - [super didReceiveMemoryWarning]; -} - -- (void)scrollViewDidScroll:(UIScrollView *)scrollView { - NSInteger newIndex = self.scrollView.contentOffset.x / self.scrollView.frame.size.width; - if (newIndex!=self.currentIndex){ - self.currentIndex = newIndex; - [self layoutViews]; - } -} - (void)layoutViews { CGPoint savedOffset = self.scrollView.contentOffset; @@ -146,24 +142,32 @@ self.scrollView.autoresizesSubviews = NO; self.scrollView.contentOffset = savedOffset; - + NSInteger baseIndex = MAX(0,self.currentIndex-1); NSInteger z = baseIndex; for ( NSInteger i = baseIndex; i < MIN(baseIndex+2, self.extractedAttachments.count);i++ ){ UIImageView *imageView = self.imageViews[z%self.imageViews.count]; BITFeedbackMessageAttachment *attachment = self.extractedAttachments[i]; - imageView.image =[attachment imageRepresentation]; + imageView.image = [self imageForAttachment:attachment]; imageView.frame = [self frameForItemAtIndex:i]; z++; } - + } -- (BOOL)prefersStatusBarHidden { - return self.navigationController.navigationBarHidden; +#pragma mark - UIScrollViewDelegate + +- (void)scrollViewDidScroll:(UIScrollView *)scrollView { + NSInteger newIndex = self.scrollView.contentOffset.x / self.scrollView.frame.size.width; + if (newIndex!=self.currentIndex){ + self.currentIndex = newIndex; + [self layoutViews]; + } } +#pragma mark - IBActions + - (void)close:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } @@ -175,6 +179,8 @@ [self presentViewController:activityVC animated:YES completion:nil]; } +#pragma mark - UIGestureRecognizer + - (void)tapped:(UITapGestureRecognizer *)tapRecognizer { if (self.navigationController.navigationBarHidden){ [UIView animateWithDuration:0.35f animations:^{ @@ -191,8 +197,21 @@ [self layoutViews]; } +#pragma mark - Layout Helpers + - (CGRect)frameForItemAtIndex:(NSInteger)index { return CGRectMake(index * CGRectGetWidth(self.scrollView.frame), 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.view.bounds)); } +- (UIImage *)imageForAttachment:(BITFeedbackMessageAttachment *)attachment { + UIImage *cachedObject = self.images[@([self.extractedAttachments indexOfObject:attachment])]; + + if (!cachedObject){ + cachedObject = [attachment imageRepresentation]; + self.images[@([self.extractedAttachments indexOfObject:attachment])] = cachedObject; + } + + return cachedObject; +} + @end diff --git a/Classes/BITImageAnnotationViewController.m b/Classes/BITImageAnnotationViewController.m index 0513544e55..69e2359053 100644 --- a/Classes/BITImageAnnotationViewController.m +++ b/Classes/BITImageAnnotationViewController.m @@ -85,7 +85,8 @@ typedef NS_ENUM(NSInteger, BITImageAnnotationViewControllerInteractionMode) { self.imageView.image = self.image; self.imageView.contentMode = UIViewContentModeScaleToFill; - + self.view.frame = UIScreen.mainScreen.applicationFrame; + [self.view addSubview:self.imageView]; // Erm. self.imageView.frame = [UIScreen mainScreen].bounds;