Así que he estado tratando de averiguar qué estoy haciendo mal desde hace un tiempo y no puedo resolverlo. Lo que estoy tratando de lograr es la siguiente:El cambio de tamaño de UIImage no funciona correctamente
- tomar una foto con
UIImagePickerController
- Tome la foto resultante y recortar la parte superior e inferior por lo que se convierte en un cuadrado (similar a Instagram)
- de visualización que imagen dentro de un
UIButton
Por alguna razón, cada vez que tome la foto que termina distorsionada dentro del UIButton
y parece como si el cultivo no funcionaba correctamente. Esto es lo que hago. Dentro del método didFinishPickingMediaWithInfo
Tengo el siguiente código:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Copy the image to the userImage variable
[picker dismissModalViewControllerAnimated:YES];
userImage = nil;
//Rotate & resize image
userImage = [self resizeAndRotatePhoto:(UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]];
NSLog(@"Userimage size, width: %f , height: %f", userImage.size.width , userImage.size.height);
//Update the image form field with the image and set the image in the controller
NSLog(@"Button size is height: %f , width: %f" , userImageAvatarButton.frame.size.height , userImageAvatarButton.frame.size.width);
[userImageAvatarButton.layer setMasksToBounds:YES];
[userImageAvatarButton.layer setCornerRadius:3.0];
[userImageAvatarButton setImage:userImage forState:UIControlStateNormal];
}
voy a incluir el método resizeAndRotatePhoto momentáneamente pero el resultado está por debajo. Además, en el código anterior, @property (strong) (UIImage *)userImage;
se define en el archivo de encabezado ViewController. La salida de registro también da como resultado:
2012-05-07 17:38:07.995 NewApp[10666:707] Userimage size, width: 1936.000000 , height: 1936.000000
2012-05-07 17:38:08.000 NewApp[10666:707] Button size is height: 60.000000 , width: 60.000000
Como ve en la imagen siguiente, termina distorsionada.
En cuanto al método resizeAndRotate, aquí está:
- (UIImage *)resizeAndRotatePhoto:(UIImage *)source
{
if(source.imageOrientation == UIImageOrientationRight)
{
source = [self rotateImage:source byDegrees:90];
}
if(userImage.imageOrientation == UIImageOrientationLeft)
{
source = [self rotateImage:source byDegrees:-90];
}
CGFloat x,y;
CGFloat size;
if(source.size.width > source.size.height){
size = source.size.height;
x = (source.size.width - source.size.height)/2;
y = 0;
}
else {
size = source.size.width;
x = 0;
y = (source.size.height - source.size.width)/2;
}
CGImageRef imageRef = CGImageCreateWithImageInRect([source CGImage], CGRectMake(x,y,size,size));
return [UIImage imageWithCGImage:imageRef];
}
En este punto, no tengo idea de cómo obtener esta imagen para aparecer sin distorsiones. Parece estar recortándolo mal y mostrándolo mal a pesar de que el sistema dice que la imagen es de hecho un cuadrado.
Gracias. Después de horas y horas de frustración y búsqueda en línea, probando cerca de 20 soluciones propuestas, esta es la ÚNICA solución que ha funcionado. Muchas gracias. – thephatp
yup. me ahorró horas de trabajo también. Aprecio la categoría! – kevinl