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に変更してFitに変更するだけです;)

- (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 に答える