40 votes

image d'arrière-plan iOS UIView

Je veux avoir un UIImagecomme image d'arrière-plan sur un UIView.

Voici mon code :

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login.png"]];
self.view.backgroundColor = background;
[background release];

Le problème, c'est que l'échelle n'est pas correcte. Il a mis à l'échelle 640 à 480, donc je ne peux pas lui dire 100% sûr, mais il semble que seulement 300 à 250 est affiché ou quelque chose comme ça.

Y a-t-il un ajustement à l'échelle / ajustement à l'UIView / ajustement à la taille ?

54voto

uneakharsh Points 369

vous devez traiter votre image avant de l'ajouter, essayez ceci :

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:image];

29voto

Dipak Chaudhari Points 492

bonjour essayez ceci,

     self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]];

11voto

Spécifiez un arrière-plan réel et envoyez-le à l'arrière de votre vue

//add background
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
[myView addSubview:background];
[myView sendSubviewToBack:background];
myView.contentMode = UIViewContentModeScaleAspectFit;

9voto

Zaid Khan Points 15

Dans SWIFT, utilisez ce...

    UIGraphicsBeginImageContext(self.view.frame.size)
    UIImage(named: "1.png")?.drawInRect(self.view.bounds)

    var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    self.view.backgroundColor = UIColor(patternImage: image)

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X