【问题标题】:Resizing UIImage in UIImageView在 UIImageView 中调整 UIImage 的大小
【发布时间】:2011-09-16 22:03:49
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

  • 您是否检查了 CGImageGetColorSpace(imageRef) 返回的值是否是 CGBitmapContextCreate 接受的值之一?

标签: iphone xcode uiimageview


【解决方案1】:
UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);

【讨论】:

    【解决方案2】:

    在快速的情况下

    imageView.contentMode = .ScaleAspectFill
    imageView.clipsToBounds = true
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 感谢您的回答。 clipsToBounds 是我所缺少的 - 巨大的帮助!
      • 这将正确地用图像填充视图,同时保持其纵横比,但隐藏不适合该区域的内容。
      【解决方案4】:

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

          myImageView.contentMode  = UIViewContentModeScaleAspectFit;
      

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

      resizing a UIImage without loading it entirely into memory?

      UIImage: Resize, then Crop

      【讨论】:

        猜你喜欢
        • 2013-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 1970-01-01
        • 2013-03-05
        • 1970-01-01
        • 2021-12-28
        相关资源
        最近更新 更多