Ashin Mandal
Ashin Mandal

Reputation: 463

How to display an image using UIView in XCode?

Absolutely beginner question. I am trying to make an app which will switch an array of images by swiping the screen sideways. So, as vanilla case of that I was trying to display an image first and then think about how to switch between images using an action. I am using XCode 4.2.

So, here's what I have so far. I have added a UIImageView to my storyboard, then Ctrl+dragged into the ".h" file to create an Outlet and it looks something like this:

SwitchViewController.h

#import <UIKit/UIKit.h>

@interface SwitchViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;


@end

And then in the ".m" file, I am trying to set an image to the UIImageView in the DidLoad method.

SwitchViewController.m

#import "SwitchViewController.h"

@implementation SwitchViewController
@synthesize imageView;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIImage *plate1 = [UIImage imageNamed:@"plates1.tif"];
    [imageView setImage:plate1];
}

- (void)viewDidUnload
{
    [self setImageView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ((interfaceOrientation == UIInterfaceOrientationLandscapeRight) ||

        (interfaceOrientation == UIInterfaceOrientationLandscapeLeft))

        return YES;



    return NO;
}

@end

Now, if I try to run this, there are no compilation errors.
But the program halts saying, Thread 1:Program received signal "SIGABRT"

Upvotes: 3

Views: 15999

Answers (4)

mattjgalloway
mattjgalloway

Reputation: 34912

Referring to your actual problem, you're getting this exception being thrown:

2012-01-26 22:04:40.728 Switch-a-Switch[548:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<SwitchViewController 0x6840660> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key UIImageView.'

It would appear that you've wired up the UIImageView to a property called UIImageView rather than imageView. Go and redo the connection in Interface Builder and make sure you drag from the imageView property to your UIImageView instance.

Upvotes: 1

Grady Player
Grady Player

Reputation: 14549

UIImage *plate1 = [UIImage imageNamed:@"plates1.tif"];
[imageView setImage:plate1]; 

should be kosher in iOS and 64 Bit (non-fragile ABI environments, as the iVar will be synthesized), I think it is much better form to include the ivar in the interface....

@interface SwitchViewController : UIViewController
{
    UIImageView *imageView;
}

The thing that you should do to track this down is to turn on zombies and enable an exception breakpoint.

Upvotes: 1

GWed
GWed

Reputation: 15653

I think you may be going about this the wrong way. If you want to side swip to go between images, you should use a UIScrollView with paging enabled. A quick google came up with this.

Hope that helps

Upvotes: 0

glogic
glogic

Reputation: 4092

afew things there. (now im only learning myself so may be wrong but here goes)

when you use the @property u should use the self.imageView setter to assign to it. so [imageView setImage:plate1] would become self.imageView.image = plate1. and change it from weak to retain in the .h. and in the viewdidunload do self.imageView = nil. also i think the swiping of images effect your trying to achieve here might be better suited to a horizontal scrollview. and u can turn on paging or something for that. hope that helps and didnt send u completely wrong direction :P

Upvotes: 0

Related Questions