xcode 在 UIImageView 中调整 UIImage 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6381612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:59:05  来源:igfitidea点击:

Resizing UIImage in UIImageView

iphonexcodeuiimageview

提问by Paul Woidke

I'm trying to create a UIPickerView with some images in it, but I can't seem to figure out how to get the images to fit in the view (right now they're too large and are overlapping each other).

我正在尝试创建一个包含一些图像的 UIPickerView,但我似乎无法弄清楚如何让图像适合视图(现在它们太大并且相互重叠)。

I'm trying to use a function to resize each image when it's drawn, but I'm getting errors when the function is called, although the program compiles and runs fine (with the exception of the image not resizing). The resizing function and initialization functions are:

我正在尝试使用一个函数在绘制每个图像时调整每个图像的大小,但是在调用该函数时出现错误,尽管程序编译并运行良好(图像未调整大小除外)。调整大小函数和初始化函数是:

-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {
    NSLog(@"resizing");
    CGImageRef imageRef = [image CGImage];
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);

    //if (alphaInfo == kCGImageAlphaNone)
    alphaInfo = kCGImageAlphaNoneSkipLast;

    CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), 
                                                4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
    CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return result;  
}

- (void)viewDidLoad {
    UIImage *h1 = [UIImage imageNamed:@"h1.png"];

    h1 = [self resizeImage:h1 width:50 height: 50];

    UIImageView *h1View = [[UIImageView alloc] initWithImage:h1];

    NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
                                h1View, nil];

    NSString *fieldName = [[NSString alloc] initWithFormat:@"column1"];
    [self setValue:imageViewArray forKey:fieldName];
    [fieldName release];
    [imageViewArray release];

    [h1View release];
}

Console Output:

控制台输出:

TabTemplate[29322:207] resizing

TabTemplate[29322] : CGBitmapContextCreate: unsupported colorspace

TabTemplate[29322] : CGContextDrawImage: invalid context

TabTemplate[29322] : CGBitmapContextCreateImage: invalid context

TabTemplate[29322:207] 调整大小

TabTemplate[29322]:CGBitmapContextCreate:不支持的色彩空间

TabTemplate[29322]:CGContextDrawImage:上下文无效

TabTemplate[29322]:CGBitmapContextCreateImage:上下文无效

I can't figure out what's going wrong. Any help is greatly appreciated.

我无法弄清楚出了什么问题。任何帮助是极大的赞赏。

回答by Jhaliya

You don't require to resize your UIImageif you use the contentModeproperty of UIImageView.

你并不需要来调整你的UIImage,如果你使用contentMode的财产UIImageView

    myImageView.contentMode  = UIViewContentModeScaleAspectFit;

Or if you still want to resize your UIImage, Have look at below SO post.

或者,如果您仍想调整 UIImage 的大小,请查看以下 SO 帖子。

resizing a UIImage without loading it entirely into memory?

调整 UIImage 的大小而不将其完全加载到内存中?

UIImage: Resize, then Crop

UIImage:调整大小,然后裁剪

回答by trillions

Use below to scale the image using aspect ratio, then clip the image to imageview's bounds.

使用下面使用纵横比缩放图像,然后将图像剪辑到图像视图的边界。

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES; 

回答by Ghulam Rasool

In case of swift

快速的情况下

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true

回答by James Bush

UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);