iPhone dev: tout extrait pour resize une image et conserver le ratio?

Je suis nouveau à développer du code dans l'iPhone, et je veux chercher du code pour resize mon UIImage à une taille spécifiée mais garder le ratio. La taille spécifiée est quelque chose comme un cadre que l'image ne peut pas franchir la limite, dans cette limite l'image doit s'adapter au cadre et conserver le ratio, le code que j'utilise actuellement peut resize mais ne peut pas conserver le ratio, il suffit de le coller ici pour voir si je peux faire quelques modifications sortingviales afin qu'il puisse le rendre possible.

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize { CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height)); CGImageRef imageRef = image.CGImage; UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); // Set the quality level to use when rescaling CGContextSetInterpolationQuality(context, kCGInterpolationHigh); CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height); CGContextConcatCTM(context, flipVertical); // Draw into the context; this scales the image CGContextDrawImage(context, newRect, imageRef); // Get the resized image from the context and a UIImage CGImageRef newImageRef = CGBitmapContextCreateImage(context); UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; CGImageRelease(newImageRef); UIGraphicsEndImageContext(); return newImage; 

}

Eh bien, vous pourriez resize l'image un peu plus simple:

 + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

Ensuite, vous devez calculer une nouvelle taille qui maintient le ratio d'aspect. Par exemple, pour get une image de la moitié de la taille, vous devez fournir une taille créée comme ceci:

  CGSize halfSize = CGSizeMake(image.size.width*0.5, image.size.height*0.5); 

Pseudo code pour resize les images à des limites spécifiques tout en préservant les proportions:

 imageSize // The image size, for example {1024,768} boundarySize // The boundary to fit the image into, for example {960,640} boundaryAspectRatio = boundarySize.width / boundarySize.height imageAspectRatio = imageSize.width / imageSize.height if ( imageAspectRatio == boundaryAspectRatio ) { // The aspect ratio is equal // Resize image to boundary } else if ( imageAspectRatio > boundaryAspectRatio ) { // The image is wider // Resize to: // - Width: boundarySize.width // - Height: boundarySize.height / imageAspectRatio } else if ( imageAspectRatio < boundaryAspectRatio ) { // Resize to: // - Width: boundarySize.width * imageAspectRatio // - Height: boundarySize.height } 

Remplacez simplement MAX par MIN au lieu de Remplir;)

 - (UIImage *)imageByFillingSize:(CGSize)newSize useScreenScale:(BOOL)useScreenScale { CGSize size = [self size]; CGRect frame; float ratioW = newSize.width / size.width; float ratioH = newSize.height / size.height; float ratio = MAX(ratioW, ratioH); frame.size.width = size.width * ratio; frame.size.height = size.height * ratio; frame.origin.x = (newSize.width - frame.size.width) / 2.0; frame.origin.y = (newSize.height - frame.size.height) / 2.0; UIGraphicsBeginImageContextWithOptions(newSize, YES, useScreenScale ? 0.0 : 1.0); [self drawInRect:frame]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }