Este hilo es viejo, pero es lo que me sacó cuando se trata de resolver este problema. Una vez que la imagen se escala, no se mostraba bien en mi contenedor a pesar de que desactivaba el diseño automático. La forma más fácil para mí de resolver esto para mostrar en una fila de la mesa, era pintar la imagen sobre un fondo blanco que tenía un tamaño fijo.
Función de ayuda
+(UIImage*)scaleMaintainAspectRatio:(UIImage*)sourceImage :(float)i_width :(float)i_height
{
float newHeight = 0.0;
float newWidth = 0.0;
float oldWidth = sourceImage.size.width;
float widthScaleFactor = i_width/oldWidth;
float oldHeight = sourceImage.size.height;
float heightScaleFactor = i_height/oldHeight;
if (heightScaleFactor > widthScaleFactor) {
newHeight = oldHeight * widthScaleFactor;
newWidth = sourceImage.size.width * widthScaleFactor;
} else {
newHeight = sourceImage.size.height * heightScaleFactor;
newWidth = oldWidth * heightScaleFactor;
}
// return image in white rect
float cxPad = i_width - newWidth;
float cyPad = i_height - newHeight;
if (cyPad > 0) {
cyPad = cyPad/2.0;
}
if (cxPad > 0) {
cxPad = cxPad/2.0;
}
CGSize size = CGSizeMake(i_width, i_height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), YES, 0.0);
[[UIColor whiteColor] setFill];
UIRectFill(CGRectMake(0, 0, size.width, size.height));
[sourceImage drawInRect:CGRectMake((int)cxPad, (int)cyPad, newWidth, newHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
// will return scaled image at actual size, not in white rect
// UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight));
// [sourceImage drawInRect:CGRectMake(0, 0, newWidth, newHeight)];
// UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
// return newImage;
}
me llamaron a este como esto de mi mesa vista cellForRowAtIndexPath
PFFile *childsPicture = [object objectForKey:@"picture"];
[childsPicture getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *largePicture = [UIImage imageWithData:imageData];
UIImage *scaledPicture = [Utility scaleMaintainAspectRatio:largePicture :70.0 :70.0 ];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.image = scaledPicture;
[self.tableView reloadData];
}
}];
Ver http://stackoverflow.com/questions/2658738/the-simplest-way-to- resize-an-uiimage – Costique
Escribí un código para escalar una imagen por un factor de dos al caminar a través de los píxeles y sumar 4 píxeles en uno. Funcionó bastante bien (mejor calidad de imagen que usar la escala del sistema), pero tal vez tenía 50 líneas de código y no era realmente bonita. (Entonces descubrí que no necesitaba escalar la imagen de todos modos.) –
Aquí está el hilo donde se puede encontrar mi algoritmo: http: // stackoverflow.com/questions/6052188/high-quality-scaling-of-uiimage –