2

我是在 iPhone 中开发代码的新手,我想搜索一些代码以将我的 UIImage 调整为指定大小但保持比例。指定的大小类似于图像不能越过边界的框架,在该边界内,图像应该缩放以适合框架并保持比例,我目前使用的代码可以调整大小但不能保持比率,只需将其粘贴在这里,看看我是否可以进行一些琐碎的修改,使其成为可能。

- (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;

}

4

3 回答 3

2

好吧,您可以更简单地调整图像的大小:

+ (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;
}

然后你需要计算一个新的尺寸来维持纵横比。例如,要获取图像大小的一半,您将提供这样创建的大小:

  CGSize halfSize = CGSizeMake(image.size.width*0.5, image.size.height*0.5);
于 2012-06-22T16:48:38.187 回答
2

用于在保持纵横比的同时将图像大小调整为特定边界的伪代码:

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
} 
于 2012-06-22T16:50:33.080 回答
1

只需将 MAX 更改为 MIN 以适合而不是填充 ;)

- (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;
}
于 2012-06-22T16:54:06.337 回答