简体   繁体   中英

How can I load an image on a View Controller window?

(Note: when I first started my project I chose to use StoryBoards so I don't seem to have access to any .nib files, in case such access will be needed for what I'm asking for here..)

I want to create a method that takes an image and the necessary coordinate parameters and loads an image at a specific position on my current View Controller's window whenever I call that method.

I'm not asking for the exact method obviously because for the time being I can't even load one single image! :)

I haven't experimented with graphics on iOS before but I went through the documentation and it was quite confusing so I decided to ask for some clarifications that might help me and others on the matter.

Apparently, I need to create an ImageView object that will hold the image I want inside it. However, there is no mention in the documentation on how to actually & eventually load the image on the window. Also, there is no actual explanation about what a superView is . Is the SuperView the first UIView object that I will be creating or is it created for me by the system and I can have access to it by calling self.view ? Do I need to add each UIView object I'm creating as a subclass to that first SuperView? By subclassing view am I defining the way in which they will overlap each other on the screen?

Here's the code I'm using, for testing purposes, trying to load one image on the screen:

#import "TestViewController.h"

@interface TestViewController ()
{
    UIView *myView;            
    UIImage *myImage;
    UIImageView *myImageView;
}

@end

@implementation TestViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) 
    {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // defining the size of my UIView..
    CGRect  viewRect = CGRectMake(10, 10, 100, 100);

    // creating the UIView object
    myView = [[UIView alloc] initWithFrame:viewRect];

    // loading my image (a red square) into a UIImage object..
    myImage = [[UIImage alloc] initWithContentsOfFile:(@"userCell.png")];

    // creating a UIImageView object that will hold my myImage..
    myImageView = [[UIImageView alloc] initWithImage:myImage];

    // trying to make my Image load to the screen by adding the object that is 
    // encapsulating it to the UIView I created..

    [myView addSubview:myImageView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Thanks!

This way:

CGRect  viewRect = CGRectMake(10, 10, 100, 100);
myImageView = [[UIImageView alloc] initWithImage:myImage];
[myImageView setFrame:viewRect];
[self.view addSubview:myImageView];

Basically you need to define the frame the subview (in your case a UIImageView) will be displayed, then add this view to your view controller's main view (that's what self.view is). No need to wrap the ImageView in a UIView, since UIImageView is a UIView itself.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // defining the size of my UIView..
    CGRect  viewRect = CGRectMake(10, 10, 100, 100);

    // creating the UIView object
    myView = [[UIView alloc] initWithFrame:viewRect];

    // loading my image into a UIImage object..
    myImage = [UIImage imageNamed:@"userCell.png"];

    // creating a UIImageView object that will hold my myImage..
    myImageView = [[UIImageView alloc] initWithImage:myImage];

    // trying to make my Image load to the screen by adding the object that is 
    // encapsulating it to the UIView I created..

    [myView addSubview:myImageView];
    [self.view addSubView:myView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM