iphone - 在 UIImageView 中调整 UIImage 的大小

标签 iphone xcode uiimageview

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

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

-(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];
}

控制台输出:

TabTemplate[29322:207] resizing

TabTemplate[29322] : CGBitmapContextCreate: unsupported colorspace

TabTemplate[29322] : CGContextDrawImage: invalid context

TabTemplate[29322] : CGBitmapContextCreateImage: invalid context

我不知道出了什么问题。非常感谢任何帮助。

最佳答案

如果您使用 UIImageViewcontentMode 属性,则不需要调整 UIImage 的大小。

    myImageView.contentMode  = UIViewContentModeScaleAspectFit;

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

resizing a UIImage without loading it entirely into memory?

UIImage: Resize, then Crop

关于iphone - 在 UIImageView 中调整 UIImage 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381612/

相关文章:

ios - 为 UIImagePicker 添加圆形裁剪组件?

ios - 如何找到 ImageView 的半径?

iphone - 按下后退按钮时如何取消或停止线程?

ios - 重新安装 xcode 模拟器运行时

ios - 在 Swift 中制作 Gif 动画

ios - 如何快速将 UTC 时间转换为 Unix 时间戳?

IOS - XCODE - 将 IPA 转换为 Swift 代码

iphone - Ruby on Rails View 的移动版本

ios - 如何设置 MKMap View 的缩放级别使 MKCircle(可变半径)可见?

ios - 如何使用帮助正则表达式检查给定的 Url 是 "Youtube " "Vimeo"和 "Unknow "视频 URL 。在 objective-c IOS中