Search code examples
iphoneobjective-cuiimagensurlimage

How to fix the emplacement of the picture in xcode


I'm playing with the tabbar, navigationBar and SegmentedBar in my application.

I have to show an picture from a HTTP like this :

- (void)viewDidLoad {
    [super viewDidLoad];
    // build the URL, perform the request and show the picture
    NSURL *url = [NSURL URLWithString: @"http://api.clementhallet.be/15avril.png"];

    //UIImage
    [[UIImageView alloc] initWithImage:image];
    image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
    image.frame = CGRectMake(0, 0, 100, 100);
    [self.view addSubview:[[UIImageView alloc] initWithImage:image]];
}

It's running but the picture isn't where I want [exactly][1].

Thanks to help me!


Solution

  • This should be the right order:

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; 
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    
    imageView.frame = CGRectMake(0, 0, 100, 100); 
    
    [self.view addSubview:imageView];
    

    You've got to set the frame of your imageview for positioning. If not in interface builder done, do it like this:

    image.frame = CGRectMake(x, y, width, height);