¿Cómo evitar que una imagen se estire dentro de un UIImageView?
Tengo un UIImageView
donde he establecido el tamaño del marco en x = 0, y = 0, width = 404, height = 712
.
En mi proyecto, necesito cambiar la imagen en UIImageView
dinámicamente.
Estoy usando este código para cambiar la imagen:
self.imageView.image = [UIImage imageNamed:@"setting-image.png"];
El problema es que cuando el tamaño de la imagen *.png
es menor que el tamaño del marco UIImageView
, la imagen se estira. No quiero que se estire. ¿Hay alguna manera de hacer eso?
10 answers
Puede usar
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
Swift 3:
imageView.contentMode = .scaleAspectFit
O UIViewContentModeCenter
/ .center
, o cualquiera de los otros modos descritos en la documentación de UIView.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-27 16:28:40
Configurar clipsToBounds en combinación con UIViewContentModeScaleAspectFit contentMode fue lo que hizo el truco para mí. ¡Espero que eso ayude a alguien!
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-21 15:49:55
Utilice el contentMode
propiedad. Probablemente quieras UIViewContentModeScaleAspectFit
o UIViewContentModeCenter
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 04:21:32
Cambie el modo de UIImage de 'escalar para llenar' a 'Centrar' dentro del creador de interfaces. Asegúrate de que tu imagen tenga el tamaño exacto que necesitas.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-07 14:25:01
Debe establecer CGSize como el ancho y la altura de la imagen para que la imagen no se extienda y se organice en el medio de imageview.
- (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size
{
CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
CGFloat width = image.size.width * scale;
CGFloat height = image.size.height * scale;
CGRect imageRect = CGRectMake((size.width - width)/2.0f,
(size.height - height)/2.0f,
width,
height);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
[image drawInRect:imageRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-11 09:36:04
Actualización para Swift 3:
imageView.contentMode = .scaleAspectFit
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-25 20:41:23
Cambie el modo de contenido, por ejemplo:
imageView.contentMode = UIViewContentModeScaleAspectFit;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-12 04:20:34
Cómo usar el tamaño original de la imagen - sin ningún estiramiento
- Simplemente cambie el Modo de UIImage de
scale to fill
aAspect Fit
dentro del creador de interfaces.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-24 22:45:33
Use esto para su UIImageView
imageView.contentMode = UIViewContentModeScaleAspectFill;
No obtendrá ningún espacio y con escala preservada. Sin embargo, alguna parte de la imagen será recortada.
Si utiliza lo siguiente:
imageView.contentMode = UIViewContentModeScaleAspectFit;
Habrá algún espacio vacío, pero la escala se conserva.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-06 13:17:12
Todavía se estira cuando la imagen es más grande que imageivew
Swift
let qCodeImage = UIImage(named: "qcode-placeholder.png")!
let qCodeImageView = UIImageView(frame: CGRectMake(0, 0, CGRectGetWidth(cardView.frame)-30, CGRectGetWidth(cardView.frame)-30))
qCodeImageView.image = qCodeImage
qCodeImageView.clipsToBounds = true
qCodeImageView.contentMode = UIViewContentMode.ScaleToFill
qCodeImageView.center = cardView.center
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-02 10:11:26