views:

96

answers:

4

Hello People,

i have implemented a small method that imports an picture on the iPhone screen, but the picture is a little bit to big for the screen. This is the Method:

- (void)ladeImage {
    id path = @"http://172.23.1.63:8080/RestfulJava/pics";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];  
    UIImage *img = [[UIImage alloc] initWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:imgView];

}

But is there any method to scall the picture on the iPhone screen?

Greetings and thank you all for Helping Marco

+1  A: 

Just change the frame to the screen size, i.e. imgView.frame = self.view.frame

Nickolay O.
Yes that works nearly, but when i turn around the iPhone to landscape, then it isn't on the screen, you understand me?
Marco
+1  A: 

hi,

you should insert following code before inserting imageview to the view.

[imgView setContentMode:UIViewContentModeScaleToFill]

Also you can set/change from ScaleToFill to AspectFill or as you needed.

and problem will be soved.

iPhone Fun
Ok thank for your helping, but this method doesn't work, when i switch the iPhone to landscape, her is the code with your tipp:- (void)ladeImage { id path = @"http://172.23.1.63:8080/RestfulJava/pics"; NSURL *url = [NSURL URLWithString:path]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *img = [[UIImage alloc] initWithData:data]; UIImageView *imgView = [[UIImageView alloc] initWithImage:img]; [imgView setContentMode:UIViewContentModeScaleToFill]; imgView.frame = self.view.frame; [self.view addSubview:imgView];}
Marco
for that you will have to set the frame of the imageview like imgView.frame = CGRectMake(0,0, 480,320) for landscape and CGRectmake(0,0, 320,480) for Portrait mode. Hop you'll know how to find landscape and portrait mode method for iphone.
iPhone Fun
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { toOrientation= interfaceOrientation; return YES;} before return true write frame for the imgView.
iPhone Fun
Sorry i am a total newbie on iPhone engineering and i can't follow your help, i don't know where i have to implement what, sorry :(
Marco
no worries man. simply add above code in your application. make the frame of imageview as I provided. also implement following code if(toOrientation == UIInterfaceOrientationPortrait || toOrientation == UIInterfaceOrientationPortraitUpsideDown) { ImgMainPhoto.frame = CGRectMake(0, 0, 320, 480); //[self PortraitMode]; } else if((toOrientation == UIInterfaceOrientationLandscapeLeft ) || (toOrientation == UIInterfaceOrientationLandscapeRight)) { ImgMainPhoto.frame = CGRectMake(0, 0, 480, 320); //[self LendscapreMode]; } in to the method viewWillAppear
iPhone Fun
and this code i have to implement in the class.m where the viewDidLoad of this controller is??
Marco
look this is my class:
Marco
i have answered down because there is more place :)
Marco
now i am worried :)
Marco
A: 

Ok thank for your helping, but this method doesn't work, when i switch the iPhone to landscape, her is the code with your tipp:

- (void)ladeImage {
    id path = @"http://172.23.1.63:8080/RestfulJava/pics";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];  
    UIImage *img = [[UIImage alloc] initWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [imgView setContentMode:UIViewContentModeScaleToFill];
    imgView.frame = self.view.frame;
    [self.view addSubview:imgView];
}
Marco
A: 

look this is my class:

- (void)viewDidLoad {
    [self ladeImage];

    [super viewDidLoad];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
     if(toOrientation == UIInterfaceOrientationPortrait || toOrientation == UIInterfaceOrientationPortraitUpsideDown) 
     { 
         ImgMainPhoto.frame = CGRectMake(0, 0, 320, 480); 
         [self PortraitMode];
     } else if((toOrientation == UIInterfaceOrientationLandscapeLeft ) || (toOrientation == UIInterfaceOrientationLandscapeRight)) { 
         ImgMainPhoto.frame = CGRectMake(0, 0, 480, 320); 
         [self LendscapreMode];
     }
}

- (void)ladeImage {
    id path = @"http://172.23.1.63:8080/RestfulJava/pics";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];  
    UIImage *img = [[UIImage alloc] initWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = self.view.frame;
    imgView.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:imgView];
}
Marco
remove [self PortraitMode]; and [self LendscapreMode]; it's mine own methods, it will create error
iPhone Fun