346

私は UIImageView を持っています。目的は、高さまたは幅を指定して比例的に縮小することです。

UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

//Add image view
[self.view addSubview:imageView];   

//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;

//change width of frame
CGRect frame = imageView.frame;
frame.size.width = 100;
imageView.frame = frame;

画像のサイズが変更されましたが、位置が左上ではありません。image/imageView をスケーリングする最良の方法と、位置を修正するにはどうすればよいですか?

4

17 に答える 17

543

ドキュメントを見つけたら、簡単に修正できました。

 imageView.contentMode = .scaleAspectFit
于 2010-02-20T00:29:24.490 に答える
403

スケール タイプに関する会話を少し見たので、最も人気のあるコンテンツ モード スケーリング タイプのいくつかに関する記事をまとめることにしました。

関連する画像は次のとおりです。

ここに画像の説明を入力

于 2013-01-08T17:22:43.290 に答える
78

これを試したところ、UIImage は _imageScaledToSize をサポートしていません。

カテゴリを使用してUIImageにメソッドを追加することになりました-Apple Devフォーラムで見つけた提案です。

プロジェクト全体の .h -

@interface UIImage (Extras)
- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize;
@end;

実装:

@implementation UIImage (Extras)

- (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize {

    UIImage *sourceImage = self;
    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;

    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;

    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;

        if (widthFactor < heightFactor) 
            scaleFactor = widthFactor;
        else
            scaleFactor = heightFactor;

        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;

        // center the image

        if (widthFactor < heightFactor) {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
        } else if (widthFactor > heightFactor) {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }


    // this is actually the interesting part:

    UIGraphicsBeginImageContext(targetSize);

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [sourceImage drawInRect:thumbnailRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    if(newImage == nil) NSLog(@"could not scale image");


    return newImage ;
}

@end;
于 2009-02-11T16:44:09.903 に答える
39
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
于 2012-12-11T07:16:54.993 に答える
30

imageViewサイズをに合わせてみてくださいimage。次のコードはテストされていません。

CGSize kMaxImageViewSize = {.width = 100, .height = 100};
CGSize imageSize = image.size;
CGFloat aspectRatio = imageSize.width / imageSize.height;
CGRect frame = imageView.frame;
if (kMaxImageViewSize.width / aspectRatio <= kMaxImageViewSize.height) 
{
    frame.size.width = kMaxImageViewSize.width;
    frame.size.height = frame.size.width / aspectRatio;
} 
else 
{
    frame.size.height = kMaxImageViewSize.height;
    frame.size.width = frame.size.height * aspectRatio;
}
imageView.frame = frame;
于 2008-10-09T04:10:13.390 に答える
13

この方法でUIImageのサイズを変更できます

image = [UIImage imageWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp];
于 2011-05-25T14:55:59.300 に答える
13

これは、Swift 2.x では問題なく動作します。

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true;
于 2016-10-07T16:13:49.187 に答える
13
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 


//set contentMode to scale aspect to fit
imageView.contentMode = UIViewContentModeScaleAspectFit;

//change width of frame
//CGRect frame = imageView.frame;
//frame.size.width = 100;
//imageView.frame = frame;

//original lines that deal with frame commented out, yo.
imageView.frame = CGRectMake(10, 20, 60, 60);

...

//Add image view
[myView addSubview:imageView]; 

上部に投稿された元のコードは、iOS 4.2 でうまく機能しました。

CGRect を作成し、上、左、幅、高さのすべての値を指定することが、テーブル セル内で UIImageView を使用していた私の場合、位置を調整する最も簡単な方法であることがわかりました。(オブジェクトを解放するためのコードを追加する必要があります)

于 2011-06-15T18:10:36.373 に答える
7

スイフトの場合:

self.imageViews.contentMode = UIViewContentMode.ScaleToFill
于 2015-09-17T09:33:38.347 に答える
2

ここで提案されている解決策がうまくいかず、画像アセットが実際には PDF である場合、XCode は実際には画像ファイルとは異なる方法で PDF を扱うことに注意してください。特に、PDF を適切に埋めるために拡大縮小できないようです。代わりに、タイル状に表示されます。問題が PDF 形式であることがわかるまで、これは私を夢中にさせました。JPG に変換すれば準備完了です。

于 2017-01-05T01:04:54.673 に答える
1

通常、私は自分のアプリにこの方法を使用します ( Swift 2.x互換):

// Resize UIImage
func resizeImage(image:UIImage, scaleX:CGFloat,scaleY:CGFloat) ->UIImage {
    let size = CGSizeApplyAffineTransform(image.size, CGAffineTransformMakeScale(scaleX, scaleY))
    let hasAlpha = true
    let scale: CGFloat = 0.0 // Automatically use scale factor of main screen

    UIGraphicsBeginImageContextWithOptions(size, !hasAlpha, scale)
    image.drawInRect(CGRect(origin: CGPointZero, size: size))

    let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return scaledImage
}
于 2016-02-25T08:17:15.513 に答える
0

I think you can do something like

image.center = [[imageView window] center];
于 2008-10-09T03:04:24.693 に答える
-3

これを簡単にスケーリングする方法を次に示します。

これは 2.x でシミュレータと iPhone で動作します。

UIImage *thumbnail = [originalImage _imageScaledToSize:CGSizeMake(40.0, 40.0) interpolationQuality:1];
于 2008-10-10T22:17:54.680 に答える