Dieser Thread ist alt, aber es ist das, was ich hochgezogen, wenn dieses Problem zu lösen versuchen. Sobald das Bild skaliert wurde, wurde es in meinem Container nicht gut angezeigt, obwohl ich das automatische Layout deaktiviert habe. Der einfachste Weg für mich, dies für die Anzeige in einer Tabellenzeile zu lösen, bestand darin, das Bild auf einen weißen Hintergrund zu malen, der eine feste Größe hatte.
Helper Funktion
+(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;
}
Ich nannte dies wie folgt aus meiner Tabellenansicht 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];
}
}];
Siehe http://stackoverflow.com/questions/2658738/the-simplest-way-to- resize-an-uiimage – Costique
Ich habe Code geschrieben, um ein Bild um den Faktor zwei zu verkleinern, indem ich durch die Pixel gehe und 4 Pixel zu einem summiere. Es funktionierte ziemlich gut (bessere Bildqualität als mit der Systemskalierung), aber war vielleicht 50 Zeilen Code und nicht wirklich hübsch. (Dann entdeckte ich, dass ich das Bild sowieso nicht skalieren musste.) –
Hier ist der Thread, wo mein Algorithmus gefunden werden kann: http: // stackoverflow.com/questions/6052188/high-quality-scaling-of-uiimage –