1

我的图像高度为 600,宽度也是 600 现在我想在 UIImageView 中以 100x100 的方式以编程方式显示该图像。我尝试但图像不像原始图像我添加图像的屏幕截图第一个是 100x100,第二个是 200x200。第二个很好。但是第一个不像原始图像。知道我做什么请在此处输入图像描述

在.h

@interface ViewController : UIViewController
{
    IBOutlet UIImageView *imageView1;
    IBOutlet UIImageView *imageView2;
}
@end

英寸

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView1.frame=CGRectMake(100, 50, 100, 100);
    imageView2.frame=CGRectMake(50, 200, 200, 200);
    imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];
    imageView2.image = [UIImage imageNamed:@"Untitled-7.png"];
}

原始图像是 600x600

4

12 回答 12

18

请试试这个

UIImage *originalImage = [UIImage imageNamed:@"5.png"];
        CGSize destinationSize = CGSizeMake(100, 100);
        UIGraphicsBeginImageContext(destinationSize);
        [originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        UIImageView *newImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
        newImageView.image = newImage;
        [self.view addSubview:newImageView];
于 2013-05-08T08:55:13.687 回答
5

尝试imageView1.contentMode = UIViewContentModeScaleAspectFit;

于 2013-04-09T06:26:20.890 回答
5

使用此方法:

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

调用方法:

CGSize newSize = CGSizeMake(100.0,100.0);
[self imageWithImage:image scaledToSize:newSize];
于 2013-04-09T06:32:47.637 回答
3

设置内容模式

imageView2.contentMode = UIViewContentModeScaleAspectFit;
于 2013-04-09T06:28:54.413 回答
3

在迅速 3

func createImage(withImage image: UIImage, forSize size: CGSize) -> UIImage? {
    UIGraphicsBeginImageContext(size)
    image.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
    if let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() {
        UIGraphicsEndImageContext()
        return newImage;
    }
    return nil
}
于 2017-01-25T16:34:48.730 回答
1

从 Swift 4.2 开始,

使用它,您将获得正确纵横比的非模糊调整大小的图像 -

class func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
    let size = image.size

    let widthRatio  = targetSize.width  / size.width
    let heightRatio = targetSize.height / size.height

    // Figure out what our orientation is, and use that to form the rectangle
    var newSize: CGSize
    if(widthRatio > heightRatio) {
        newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

    //resizing the rect using the ImageContext
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage!
}

根据开发人员文档,以下方法使用指定的选项创建基于位图的图形上下文 -

func UIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat)

参数-

size:新位图上下文的大小(以磅为单位)。这表示 UIGraphicsGetImageFromCurrentImageContext() 函数返回的图像大小。

opaque:一个布尔标志,指示位图是否不透明。

scale:应用于位图的比例因子。如果您指定值 0.0,则比例因子设置为设备主屏幕的比例因子。

参考- https://developer.apple.com/documentation/uikit/1623912-uigraphicsbeginimagecontextwitho

于 2018-12-10T09:07:12.680 回答
0

用图像初始化你的 imageView,然后调整它的大小

imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Untitled-7.png"]];
imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Untitled-7.png"]];
[imageView1 setFrame:CGRectMake(100, 50, 100, 100)];
[imageView2 setFrame:CGRectMake(50, 200, 200, 200)];

试试看,让我知道它是否有效!

于 2013-04-09T06:25:08.473 回答
0

试试这个: -

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView1.frame=CGRectMake(100, 50, 100, 100);
    imageView2.frame=CGRectMake(50, 200, 200, 200);
    imageView1.contentMode = UIViewContentModeScaleAspectFit;
    imageView2.contentMode = UIViewContentModeScaleAspectFit;
    imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];
    imageView2.image = [UIImage imageNamed:@"Untitled-7.png"];
}

请享用。

于 2013-04-09T06:34:45.913 回答
0

尝试改变这个:

imageView1.image = [UIImage imageNamed:@"Untitled-7.png"];

对此:

imageView1.image = [[UIImage imageNamed:@"Untitled-7.png"] resizableImageWithCapInsets:UIEdgeInsetsZero
                                                                          resizingMode:UIImageResizingModeStretch];
于 2013-04-09T07:41:01.097 回答
0

我喜欢在 UIImage 上使用一个类别,方法如下:

// Returns a rescaled copy of the image, taking into account its orientation
// The image will be scaled disproportionately if necessary to fit the bounds specified by the parameter
- (UIImage *)resizedImage:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width * self.scale, newSize.height * self.scale));

    UIImage *newImage;
    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
    newImage = [UIImage imageWithCGImage:[self CGImage] scale:0.0 orientation:self.imageOrientation];
    [newImage drawInRect:newRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

请参阅此更新的存储库和此博客文章

这适用于调整 PDF 图像的大小,尤其是当您需要在 UIBarButtonItem 中粘贴一个时。

于 2016-12-20T21:52:33.383 回答
0

嘿,如果您已经在使用 Alamofire 图像库,那么您可以使用以下代码:

UIImage(named:"image").af_imageScaled(to: CGSize(width: 100, height: 100))
于 2017-10-04T10:43:46.513 回答