简体   繁体   English

在UIImageView中调整UIImage的大小

[英]Resizing UIImage in UIImageView

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:207]调整大小

TabTemplate[29322] : CGBitmapContextCreate: unsupported colorspace TabTemplate [29322]:CGBitmapContextCreate:不支持的颜色空间

TabTemplate[29322] : CGContextDrawImage: invalid context TabTemplate [29322]:CGContextDrawImage:无效的上下文

TabTemplate[29322] : CGBitmapContextCreateImage: invalid context TabTemplate [29322]:CGBitmapContextCreateImage:无效的上下文

I can't figure out what's going wrong. 我无法弄清楚出了什么问题。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

You don't require to resize your UIImage if you use the contentMode property of UIImageView . 如果使用UIImageViewcontentMode属性,则不需要调整UIImage大小。

    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:调整大小,然后裁剪

Use below to scale the image using aspect ratio, then clip the image to imageview's bounds. 使用下面的内容使用宽高比缩放图像,然后将图像剪切为imageview的边界。

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

In case of swift 在迅速的情况下

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true
UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM