Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Images not loading in UIScrollView for gallery app

I am building simple photo gallery app for iPhone in Xcode. For some reason when I build it labels do work, but not my images don't load. Can you look into it please




galleryviewcontroller.h

#import


@interface GalleryViewController : UIViewController {
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
NSArray *contentList;

// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) NSMutableArray *viewControllers;
@property (nonatomic, retain) NSArray *contentList;

- (IBAction)changePage:(id)sender;

-(IBAction)back:(id)sender;

@end

galleryviewcontroller.m

#import \"GalleryViewController.h\"
#import \"MyViewController.h\"






static NSString *ImageKey = @\"imageKey\";

static NSUInteger kNumberOfPages = 29;




@interface GalleryViewController (PrivateMethods)

- (void)loadScrollViewWithPage:(int)page;
- (void)scrollViewDidScroll:(UIScrollView *)sender;

@end

@implementation GalleryViewController

-(IBAction)back:(id)sender{

[self.view removeFromSuperview];

}

@synthesize scrollView, viewControllers, contentList;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
*/
- (void)viewDidLoad {




NSString *path = [[NSBundle mainBundle] pathForResource:@\"Gallery\" ofType:@\"plist\"];
self.contentList = [NSArray arrayWithContentsOfFile:path];


// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];


// a page is the width of the scroll view

scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;

pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;

// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];


[super viewDidLoad];


}

- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= kNumberOfPages) return;

// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}

// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollView addSubview:controller.view];


NSDictionary *numberItem = [self.contentList objectAtIndex:page];
controller.numberImage.image = [UIImage imageNamed:[numberItem valueForKey:ImageKey]];
}


}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a \"feedback loop\" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}

// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;

// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];

// A possible optimization would be to unload the views+controllers which are no longer visible
}

// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlUsed = NO;
}

// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}

- (IBAction)changePage:(id)sender {
int page = pageControl.currentPage;

// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];

// update the scroll view to the appropriate page
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];

// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed = YES;
}





/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (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)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[viewControllers release];
[scrollView release];
[pageControl release];
[contentList release];

[super dealloc];
}

@end

myviewcontroller.h
#import


@interface MyViewController : UIViewController {
UILabel *pageNumberLabel;
int pageNumber;
UIImageView *numberImage;
}

@property (nonatomic, retain) IBOutlet UILabel *pageNumberLabel;
@property (nonatomic, retain) IBOutlet UIImageView *numberImage;

- (id)initWithPageNumber:(int)page;

@end

myviewcontroller.m

#import \"MyViewController.h\"


@implementation MyViewController

@synthesize pageNumberLabel, numberImage;



// Load the view nib and initialize the pageNumber ivar.
- (id)initWithPageNumber:(int)page {
if (self = [super initWithNibName:@\"MyViewController\" bundle:nil]) {
pageNumber = page;
}
return self;
}



// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
pageNumberLabel.text = [NSString stringWithFormat:@\"Page %d\", pageNumber + 1];
}



- (void)dealloc {
[pageNumberLabel release];
[numberImage release];

[super dealloc];
}


@end
Post edited by oleger on

Replies

  • smithdale87smithdale87 Posts: 4,286iPhone Dev SDK Supporter
    Do you get any compiler warnings at all? Can you add some NSLogs to verify that the methods are getting called as you expect?

    Also, this catches my eye as being incorrect:

     if ((NSNull *)controller == [NSNull null]) {


    If you're going to compare objects, use isEqual:.
    It should be good enough to just check if controller isKindOfClass: NSNull class
  • jbrojbro Posts: 77Registered Users
    In your viewdidload, you set the pageNumberLabel but not the numberImage
  • olegeroleger Posts: 7New Users
    smithdale87;427086 said:
    Do you get any compiler warnings at all? Can you add some NSLogs to verify that the methods are getting called as you expect?

    Also, this catches my eye as being incorrect:

     if ((NSNull *)controller == [NSNull null]) {


    If you're going to compare objects, use isEqual:.
    It should be good enough to just check if controller isKindOfClass: NSNull class
    The only thing I'm getting is : 2012-04-23 15:20:37.381 [269:f803] Application windows are expected to have a root view controller at the end of application launch
  • olegeroleger Posts: 7New Users
    jbro;427087 said:
    In your viewdidload, you set the pageNumberLabel but not the numberImage
    I followed apples page control. numberImage is not set in viewdidload. Can you help me how to set it up?

    Tried so many things that I'm lost
  • olegeroleger Posts: 7New Users
    smithdale87;427086 said:
    Do you get any compiler warnings at all? Can you add some NSLogs to verify that the methods are getting called as you expect?

    and thats my console:

    > 
    Apr 23 15:35:22 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:com.company.app[0xd58]) Bug: launchd_core_logic.c:3732 (25562):3
    Apr 23 15:35:22 unknown com.apple.launchd[1] <Notice>: (UIKitApplication:com.company.app [0xd58]) Assuming job exited: <rdar://problem/5020256>: 10: No child processes
    Apr 23 15:35:22 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.company.app[0xd58]) Job appears to have crashed: Segmentation fault: 11
    Apr 23 15:35:22 unknown SpringBoard[52] <Warning>: Application 'AnnaRomanello' exited abnormally with signal 11: Segmentation fault: 11
    >
    Apr 23 15:35:24 unknown installd[3243] <Error>: libMobileGestalt computeUniqueDeviceID: total time for bb to return imei: 0
    Apr 23 15:35:24 unknown SpringBoard[52] <Warning>: Killing com.company.app for termination assertion
    >
    Apr 23 15:35:26 unknown installd[3243] <Error>: 2ff94000 verify_signer_identity: Could not copy validate signature: -402620393
    Apr 23 15:35:26 unknown installd[3243] <Error>: 2ff94000 load_application_info: Could not load signer identity from /private/var/mobile/Applications/CEEF2AAB-F2BE-4DD9-B67D-DA68A61DE3C7/appname.app/app
    Apr 23 15:35:27 unknown SpringBoard[52] <Warning>: Reloading application state for 'com.company.app' as its modification date or path has changed
    Apr 23 15:35:27 unknown SpringBoard[52] <Warning>: Reloading and rendering all application icons.
    >
    Apr 23 15:35:29 unknown com.apple.debugserver-64[3248] <Warning>: debugserver-64 for armv6 Copyright (c) 2007-2009 Apple, Inc. All Rights Reserved.
    Apr 23 15:35:29 unknown com.apple.debugserver-64[3248] <Warning>: Connecting to com.apple.debugserver service...
    Apr 23 15:35:30 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.company.app[0xe436]) Spawned and waiting for the debugger to attach before continuing...
    Apr 23 15:35:30 unknown com.apple.debugserver-64[3248] <Warning>: Got a connection, waiting for debugger instructions for task \"(null)\".
    Apr 23 15:35:30 unknown kernel[0] <Debug>: lockbot[3238] Builtin profile: debugserver (sandbox)
    Apr 23 15:35:30 unknown kernel[0] <Debug>: launchd[3249] Builtin profile: container (sandbox)
    Apr 23 15:35:30 unknown kernel[0] <Debug>: launchd[3249] Container: /private/var/mobile/Applications/CEEF2AAB-F2BE-4DD9-B67D-DA68A61DE3C7 [69] (sandbox)
    >
    Apr 23 15:35:31 unknown appname[3249] <Warning>: Application windows are expected to have a root view controller at the end of application launch
  • jbrojbro Posts: 77Registered Users
    oleger;427097 said:
    I followed apples page control. numberImage is not set in viewdidload. Can you help me how to set it up?

    Tried so many things that I'm lost
    I'm not familiar with Apple's page control example. I would suggest you review that code and look for how they set imageview.image and apply it to your project.
Sign In or Register to comment.