Reputation: 17
I need to be able to load images properly in both the landscape and portrait mode and for all devices. Also I need a proper memory management while loading the images. right now my code loads all the images at once but in future I will add more images so need an optimized memory solution.
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationController.navigationBarHidden = YES;
_photoArray = [NSArray arrayWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.jpg",@"image7.jpg",@"image8.jpg",@"image9.jpg", nil];
self.pageControl.numberOfPages = _photoArray.count;
CGSize screen = [[UIScreen mainScreen] bounds].size;
for(int i = 0; i < _photoArray.count; i++)
{
self.subScrollView = [[UIScrollView alloc] init];
self.subScrollView.frame = CGRectMake((i * screen.width), 0, screen.width, screen.height);
[self.subScrollView setMinimumZoomScale:1];
[self.subScrollView setZoomScale:1];
[self.subScrollView setMaximumZoomScale:3];
self.subScrollView.delegate = self;
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_photoArray[i]]];
imgView.frame = self.subScrollView.bounds;
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.clipsToBounds = YES;
imgView.tag = i;
[self.subScrollView addSubview:imgView];
[_scrView addSubview:self.subScrollView];
if(i == _photoArray.count-1)
_scrView.contentSize = CGSizeMake(self.subScrollView.frame.origin.x + self.subScrollView.frame.size.width, screen.height);
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView viewWithTag:self.pageControl.currentPage];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//page control
CGFloat pageWidth = self.scrView.frame.size.width;
self.pageControl.currentPage = (self.scrView.contentOffset.x +pageWidth/2)/pageWidth;
}
Upvotes: 0
Views: 84
Reputation: 56
I think that the best option to optimize the memory is use a UICollectionView instead UIScrollView. Also you can use UIPageControl with UICollectionView.
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.imagesArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell == [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",indexPath.row]]];
[cell.addSubView:image];
return cell;
}
Upvotes: 1