2010-09-21 20 views
1

J'ai mis en œuvre une petite méthode qui importe une image sur l'écran de l'iPhone , mais l'image est un peu trop grande pour l'écran. Ceci est la méthode:Scalling UIImage sur la taille de l'écran

- (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]; 

} 

Mais est-il une méthode à l'échelle de l'image sur l'écran de l'iPhone?

Edit1:
merci Ok pour votre aide, mais cette méthode ne fonctionne pas, quand je passe l'iPhone en mode paysage, elle est le code avec votre 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]; 
} 

Edit2:
regard c'est ma classe:

- (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]; 
} 

Répondre

3

vous devez insérer le code suivant avant d'insérer imageview à la vue.

[imgView setContentMode:UIViewContentModeScaleToFill] 

Vous pouvez également définir/changer de ScaleToFill à AspectFill ou selon vos besoins.

et le problème sera bloqué.

+0

Ok merci pour votre aide, mais cette méthode ne fonctionne pas, quand je passe l'iPhone en paysage, elle est le code avec votre tipp: - (void) ladeImage { id chemin = @ "http: //172.23 .1.63: 8080/RestfulJava/photos "; NSURL * url = [NSURL URLWithString: chemin]; NSData * data = [NSData dataWithContentsOfURL: url]; UIImage * img = [[UIImage alloc] initWithData: données]; UIImageView * imgView = [[UIImageView alloc] initImithImage: img]; [imgView setContentMode: UIViewContentModeScaleToFill]; imgView.frame = self.view.frame; [self.view addSubview: imgView]; } – Marco

+0

pour cela vous devrez définir le cadre de l'imageview comme imgView.frame = CGRectMake (0,0, 480,320) pour le paysage et CGRectmake (0,0, 320,480) pour le mode Portrait. Hop, vous saurez comment trouver la méthode de mode paysage et portrait pour iPhone. –

+0

- (BOOL) shouldAutorotateToInterfaceOrientation: (interfaceUIInterfaceOrientation) interfaceOrientation { \t toOrientation = interfaceOrientation; return YES; } avant de renvoyer le vrai cadre d'écriture pour imgView. –

1

il suffit de changer le cadre à la taille de l'écran, à savoir imgView.frame = self.view.frame

+0

Oui qui fonctionne à peu près, mais quand je tourne autour de l'iPhone en mode paysage, il est pas à l'écran, vous me comprenez? – Marco

-1

regard ceci est ma classe:

- (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]; 
} 
+0

supprimer [self PortraitMode]; et [self LendscapreMode]; c'est mes propres méthodes, il va créer une erreur –

+0

"réponse" est un supplément pour la question, en attendant inclus. – bummi