Simple UICollectionView to show images behaves odd: Some Images are displayed correct, some at wrong position, some missing at all - uiimageview

I want to show images in a grid on iPhone using a UICollectionView, showing 3 images a row. For a "dead simple test" (as I thought), I've added 15 JPG images to my project, so that they'll be in my bundle and I can load them simply via [UIImage imageNamed:...].
I think I've done everything correct (setting up & registering UICollectionViewCell subclass, use of UICollectionViewDataSource Protocol methods), however, the UICollectionView behaves very weird:
It shows only a few of the images in the following pattern:
First line shows image 1 & 3, second line is blank, next line like the first again (image 1 & 3 showing properly), fourth line blank, and so on...
If I push a button in my NavBar that triggers [self.collectionView reloadData], random cells appear or disappear. What drives me nuts is that it's not only an issue of images appear or not. Sometime, images also swap between the cells, i.e. they appear for a indexPath they are definitely not wired up!
Here is my code for the cell:
#interface AlbumCoverCell : UICollectionViewCell
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#end
#implementation AlbumCoverCell
#synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:frame];
[self.contentView addSubview:_imageView];
}
return self;
}
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
- (void)prepareForReuse
{
[super prepareForReuse];
self.imageView.image = nil;
}
#end
Part of the code for my UICollectionViewController subclass, where 'imageNames' is an NSArray holding all jpg filenames:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[AlbumCoverCell class] forCellWithReuseIdentifier:kAlbumCellID];
}
#pragma mark - UICollectionViewDataSource Protocol methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.imageNames count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AlbumCoverCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kAlbumCellID forIndexPath:indexPath];
NSString *imageName = [self.imageNames objectAtIndex:indexPath.row];
NSLog(#"CV setting image for row %d from file in bundle with name '%#'", indexPath.row, imageName);
cell.imageView.image = [UIImage imageNamed:imageName];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout Protocol methods
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
{
return CGSizeMake(100, 100);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
From the NSLog statement in cellForItemAtIndexPath: I can see that the method is called for all of the cells (not only the one's displayed) and that the mapping between indexPath.row and filename is correct.
Has anybody an idea what could cause this weird behavior?

In the meantime, I've found the solution. It was actually a very subtle error in the implementation of my UICollectionViewCell subclass AlbumCoverCell.
The problem is that I've set the frame of the cell instance as the frame of the UIImageView subview instead of passing the bounds property of the cell's contentView!
Here is the fix:
#implementation AlbumCoverCell
#synthesize imageView = _imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// WRONG:
// _imageView = [[UIImageView alloc] initWithFrame:frame];
// RIGHT:
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
}
return self;
}
- (void)prepareForReuse
{
[super prepareForReuse];
// reset image property of imageView for reuse
self.imageView.image = nil;
// update frame position of subviews
self.imageView.frame = self.contentView.bounds;
}
...
#end

Related

UIAccessibility is not selecting correct elements

I have several UIWindows in my app. Some UIWindows have very high window levels. i.e.
window.windowLevel = currentWindowLevel+1;
For some reason when turning on accessibility support, the system insists on reading out the accessibility labels of views located in the lower level window, even in cases where the views aren't even visible.
This minimal example exemplifies this behavior.
With accessibility support turned on, try to press the label in the red window. The system will read out the text from the underneath table view instead
#interface ViewController () <UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableview;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableview.dataSource = self;
[self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIWindow *w = [[UIWindow alloc] initWithFrame:self.view.bounds];
w.windowLevel = self.view.window.windowLevel + 1;
w.backgroundColor = [UIColor redColor];
w.hidden = NO;
w.isAccessibilityElement = YES;
UILabel *l = [[UILabel alloc] init];
l.text = #"KUKUKUKUKUKUKUKLU";
[l sizeToFit];
l.frame = CGRectOffset(l.frame, 40, 100);
[w addSubview:l];
w.accessibilityLabel = #"Read this outloud instead";
static id window;
window = w;
});
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 100;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
cell.textLabel.text = #"Test";
return cell;
}
#end
Solution is to set the attribute
Window.accessibilityViewIsModal = YES;

Getting a CollectionView delegate to load properly

(Let me first put the error up here so that I don't forget it: could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard)
I've got a regular UICollectionViewController that loads just fine. But I also have another controller (UserViewController) that loads a CollectionView delegate (UserPlaceViewController).
I get really confused with what I have to load in the delegate and what I have to load in the current controller. But anyway here's the delegate as-is:
#import "UserPlaceViewController.h"
#interface UserPlaceViewController ()
#end
#implementation UserPlaceViewController
#synthesize placeArray, placeTable;
- (void)viewDidLoad
{
[super viewDidLoad];
// placeTable.delegate = self;
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(145, 150);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
placeTable = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//self.collectionView.frame = CGRectMake(10, 120, self.view.bounds.size.width-20, self.view.bounds.size.height-50);
placeTable.autoresizesSubviews = YES;
placeTable.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
//placeTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
placeTable.scrollEnabled = YES;
self.view = placeTable;
NSLog(#"%lu", (unsigned long)[placeArray count]);
}
That's basically the whole thing. Couple other things at the end... but that's it for the most part.
And here are the relevent parts of the ViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES animated:YES];
UserPlaceViewController *collectionView = [[UserPlaceViewController alloc] initWithNibName:#"UserPlace" bundle:nil];
UINib * placeCell = [UINib nibWithNibName:#"Shops" bundle:nil];
[collectionView.placeTable registerNib:placeCell forCellWithReuseIdentifier:cellIdentifier];
- (void) fetchedData: (NSData *) responseData
{
UserPlaceViewController * uptvLike = [[UserPlaceViewController alloc] init];
if (IS_IPHONE5) {
uptvLike.view.frame = CGRectMake(0, 0, 320, 300);
}
else
{
uptvLike.view.frame = CGRectMake(0, 0, 320, 200);
}
uptvLike.placeTable.delegate = self;
uptvLike.placeTable.dataSource = self;
uptvLike.placeTable.layer.borderWidth = 1.0f;
uptvLike.placeTable.layer.borderColor = [UIColor colorWithRed:5/255.0f green:96/255.0f blue:255/255.0f alpha:1.0f].CGColor;
[uptvLike.placeTable reloadData];
[self.scrollView addSubview:uptvLike.view];
}
And finally:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PlaceCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
Place * p = [placeArray objectAtIndex:indexPath.item];
cell.placeName.text = p.PName;
cell.placeImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:p.PImage]]];
return cell;
}
Like I said: I know how a UICollectionView is supposed to work. I've got one running. I know you have to register a nib or class in viewDidLoad (I've registered a Nib.) But I'm confused over how exactly to load this delegate and where to load what. For example I see that I'm initialising the collectionview in both the delegate and the view controller. I'm also confused about WHERE exactly I should load the nib... (delegates give me a headache...)
So what the hell... It's raining. I figured I'd ask.
Ok. You want a UICollectionView but you also want to have a lot of regular "View" items on the same screen? Two things are important:
1. You don't need two ViewControllers. You just need to load what would normally be your UICollectionViewController as a regular ViewController:
#interface UserViewController : UIViewController <UICollectionViewDataSource, UIScrollViewDelegate>
2. Once that's done you can load your collectionview on TOP of that view:
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(145, 150);
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
[self.scrollView addSubview:self.collectionView];
[self.view addSubview:HUD];
UINib * placeCell = [UINib nibWithNibName:#"UserCell" bundle:nil];
[self.collectionView registerNib:placeCell forCellWithReuseIdentifier:#"Cell"];
The rest is just your standard CollectionView stuff.

Custom UITableViewCell does not show it's contents

In Xcode 5.1, using StoryBoards, I'm having trouble getting a custom UITableViewCell to show it's contents. The cell appears -- I can tap to select it -- but the label text doesn't appear.
I've given the Cell my custom class GCell and identifier MyCell
I've connected an IBOutlets to a label in the prototype cell.
#import <UIKit/UIKit.h>
#interface GCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *nameLabel;
#end
In my class that extends UITableViewController, I have
#implementation PayGroupViewController
- (id)initWithStyle:(UITableViewStyle)style
{
NSLog (#"[PayGroupViewController] initWithStyle");
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
// [self.tableView registerClass:[GCell class] forCellReuseIdentifier:#"MyCell"];
_groups = [NSMutableArray arrayWithCapacity:20];
GroupModel *gm = [[GroupModel alloc] init];
gm.title = #"DINNER";
gm.names = [NSMutableArray arrayWithCapacity:10];
[gm.names addObject:#"Me"];
[gm.names addObject:#"Albert"];
[_groups addObject:gm];
NSLog (#"[PayGroupViewController] viewDidLoad. [self groups].count:%d" ,[self groups].count);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog (#"[PayGroupViewController] tableView. [self groups].count:%d" ,[self groups].count);
return [self groups].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog (#"[PayGroupViewController] tableView cellForRowAtIndexPath");
GCell *c = (GCell *)[tableView dequeueReusableCellWithIdentifier:#"MyCell" forIndexPath:indexPath];
GroupModel *group = [self.groups objectAtIndex:indexPath.row];
NSLog (#"[PayGroupViewController] group.title: %#",group.title); // Outputs "DINNER"
c.nameLabel.text = group.title; // It does not show up
NSLog (#"[PayGroupViewController] cell: %#",c); // outputs <GCell: 0x9976ed0, etc. so it does exist
NSLog (#"[PayGroupViewController] cell.nameLabel: %#",c.nameLabel); // outputs (null)
return c;
}
Also, here is the entire xCode project if anyone cares to take a look.
http://www.brainjelly.com/WhoPaid.zip
Note that the prototype cell has a Disclosure Indicator Accessory, but that doesn't appear either.
Thanks… I've looked extensively over the other posts with this issue but none of those solutions helped me.
There are 2 problems in your xCode project.
You shouldn't register custom cell class in your code for prototype cells. Storyboard does that for you. When you register same class, you are basically overriding the one created by storyboard.
In your prototype cell there is an undefined outlet named "title", which is connected to Content View. Define or delete it.
After these, clear the project and recompile, it should be ok.

Page View Controller - from images to another view controller

I'm a bit perplexed about what I'm trying to accomplish. I have a page view controller that has a data source containing an array list of images. It's actually a tutorial that a user can flip through. What I'm trying to do is make the last page a log in screen so the user can enter info and hit a login button. I thought this would be as simple as adding a login view controller to the array but oooh how wrong I was D: When I tried that I got this error:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController _isResizable]: unrecognized selector sent to instance 0xa160660'
I do apologise for being such a noob I'm new to all of this just trying to get my head around it. Here's my code (accomplished by using this site actually):
My data source (ModelController.h)
#import <Foundation/Foundation.h>
#class DataViewController;
#interface ModelController : NSObject <UIPageViewControllerDataSource>
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard;
- (NSUInteger)indexOfViewController:(DataViewController *)viewController;`
#end
ModelController.m
#import "ModelController.h"
#import "DataViewController.h"
#import "LoginViewController.h"
#interface ModelController()
#property (readonly, strong, nonatomic) NSArray *pageData;
#end
#implementation ModelController
- (id)init
{
self = [super init];
if (self)
{
// Create the data model
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tutorial1.png"],
[UIImage imageNamed:#"tutorial2.png"],
[UIImage imageNamed:#"lastWishes.png"],
[UIImage imageNamed:#"todo.png"],
[UIImage imageNamed:#"web.png"],
(LoginViewController*)[[UIViewController alloc] init],
nil];
}
return self;
}
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{
// Return the data view controller for the given index.
if (([self.pageData count] == 0) || (index >= [self.pageData count]))
{
return nil;
}
// Create a new view controller and pass suitable data.
DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"DataViewController"];
dataViewController.dataObject = self.pageData[index];
return dataViewController;
}
- (NSUInteger)indexOfViewController:(DataViewController *)viewController
{
// Return the index of the given data view controller.
// For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
return [self.pageData indexOfObject:viewController.dataObject];
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageData count]) {
return nil;
}
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
#end
The Parent (RootViewController.h)
#import <UIKit/UIKit.h>
#interface RootViewController : UIViewController <UIPageViewControllerDelegate>
#property (strong, nonatomic) UIPageViewController *pageViewController;
#end
RootViewController.m
#import "RootViewController.h"
#import "ModelController.h"
#import "DataViewController.h"
#interface RootViewController ()
#property (readonly, strong, nonatomic) ModelController *modelController;
#end
#implementation RootViewController
#synthesize modelController = _modelController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil];
self.pageViewController.delegate = self;
DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0 storyboard:self.storyboard];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.pageViewController.dataSource = self.modelController;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
CGRect pageViewRect = self.view.bounds;
self.pageViewController.view.frame = pageViewRect;
[self.pageViewController didMoveToParentViewController:self];
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (ModelController *)modelController
{
// Return the model controller object, creating it if necessary.
// In more complex implementations, the model controller may be passed to the view controller.
if (!_modelController) {
_modelController = [[ModelController alloc] init];
}
return _modelController;
}
#pragma mark - UIPageViewController delegate methods
/*
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating: (BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted: (BOOL)completed
{
}
*/
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
// Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here.
UIViewController *currentViewController = self.pageViewController.viewControllers[0];
NSArray *viewControllers = #[currentViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
self.pageViewController.doubleSided = NO;
return UIPageViewControllerSpineLocationMin;
}
#end
The Child (DataViewController.h)
#import <UIKit/UIKit.h>
#interface DataViewController : UIViewController
#property (strong, nonatomic) id dataObject;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
DataViewController.m
#import "DataViewController.h"
#interface DataViewController ()
#end
#implementation DataViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView.image = _dataObject;
}
#end
Once again, the code in question is here where I'm trying to add a view controller to the data source as the last page:
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tutorial1.png"],
[UIImage imageNamed:#"tutorial2.png"],
[UIImage imageNamed:#"lastWishes.png"],
[UIImage imageNamed:#"todo.png"],
[UIImage imageNamed:#"web.png"],
(LoginViewController*)[[UIViewController alloc] init],
nil];
and getting unrecognized selector error when at runtime. I've also tried this as well:
- (id)init
{
self = [super init];
if (self)
{
LoginViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
// Create the data model
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"tutorial1.png"],
[UIImage imageNamed:#"tutorial2.png"],
[UIImage imageNamed:#"lastWishes.png"],
[UIImage imageNamed:#"todo.png"],
[UIImage imageNamed:#"web.png"],
viewController,
nil];
}
return self;
}
Any suggestions would be great. THanks!!
Your idea is 100% correct, your implementation is not.
This line:
dataViewController.dataObject = self.pageData[index];
is very suspicious because that will return a UIViewController in the case of your login screen. I would suggest you type-check your page data, if it is already a UIViewController subclass, just return it, if it is (in your case) a UIImage add it as the data object.

UIScrollView Memory-Allocation and Release

I am working on a pretty simple and straightforward app that uses a couple of pages with scroll views. I have an array of images in ViewDidLoad and then a mutable array that shows current page plus and minus 1. There is a purge page method which is supposed to remove the extra images from the superview. When running this on an actual device with a large quantity of images, a memory warning comes up after scrolling through 50 or so images and then the app crashes. I ran this through instruments and see that the memory in increasing substantially with each swipe. When creating the array I used imageNamed as well as imageWithContentsOfFile and either way gives close to the same result. I realize there have been multiple scrollview questions here, but somehow I cannot seem to get past this one. Frustrating to say the least. I hope someone can look at this with a fresh set of eyes and shed some light on my problem. Thanks very much in advance.
EDIT: Forgot to mention ARC use. Yes, I use ARC.
Code: .h file
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
#end
and the .m file
#import "FirstViewController.h"
#interface FirstViewController ()
#property (nonatomic, strong) NSArray *pageImages;
#property (nonatomic, strong) NSMutableArray *pageViews;
- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;
#end
#implementation FirstViewController
#synthesize scrollView = _scrollView;
#synthesize pageControl = _pageControl;
#synthesize pageImages = _pageImages;
#synthesize pageViews = _pageViews;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// 1
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
// 2
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
// 3
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
// 4
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 1
self.pageImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"BBD1.jpg"],
[UIImage imageNamed:#"BBD2.jpg"],
[UIImage imageNamed:#"BBD3.jpg"],
[UIImage imageNamed:#"BBD4.jpg"],
[UIImage imageNamed:#"BBD5.jpg"],
[UIImage imageNamed:#"BBD6.jpg"],
[UIImage imageNamed:#"BBD7.jpg"],
.....
[UIImage imageNamed:#"BBD292.jpg"],
[UIImage imageNamed:#"BBD293.jpg"],
[UIImage imageNamed:#"BBD294.jpg"],
[UIImage imageNamed:#"BBD295.jpg"],
[UIImage imageNamed:#"BBD296.jpg"],
[UIImage imageNamed:#"BBD297.jpg"],
[UIImage imageNamed:#"BBD298.jpg"],
[UIImage imageNamed:#"BBD299.jpg"],
[UIImage imageNamed:#"BBD300.jpg"],
nil];
NSInteger pageCount = self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 4
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// 5
[self loadVisiblePages];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
I really appreciate you taking the time to look at this and hope there is someone out there who can point me in the right direction.
THANKS!
Loading the actual images into the array incurs a much larger overhead than storing just the names in the array and loading the image like
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.pageImages objectAtIndex:page]];
UIImageView *newPageView = [[UIImageView alloc] initWithImage:image];
[image release];
At this point, only the newPageView will hold a reference to the image in memory, and when that newPageView is removed from it's superview, the image in memory will be released.
You don't say if you are using ARC or not, but without ARC there are obvious problems in memory management. In loadPage: at comment "// 3" you create newPageView with a retain count of 1, then add it as a subview of self.scrollView and to the array self.pageViews. Thus it has an overall retain count of +3. In purgePage: you remove it from self.scrollView and self.pageViews but are still left with an overall retain count of +1. Thus the UIImageView for each page is forgotten but not deallocated.
You can fix this by using ARC in your project or modifying the line where you create newPageView to be instead:
UIImageView *newPageView = [[[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]] autorelease];

Resources