在 UIImageView 中调整 UIImage 的大小

发布于 2024-11-15 21:10:22 字数 1775 浏览 2 评论 0原文

我正在尝试创建一个包含一些图像的 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:上下文无效

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

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).

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2024-11-22 21:10:22

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

    myImageView.contentMode  = UIViewContentModeScaleAspectFit;

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

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

UIImage:调整大小,然后裁剪

You don't require to resize your UIImage if you use the contentMode property of UIImageView.

    myImageView.contentMode  = UIViewContentModeScaleAspectFit;

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

resizing a UIImage without loading it entirely into memory?

UIImage: Resize, then Crop

土豪 2024-11-22 21:10:22

使用下面的方法使用宽高比缩放图像,然后将图像裁剪到图像视图的边界。

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

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

imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES; 
诗笺 2024-11-22 21:10:22

如果情况迅速

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true

In case of swift

imageView.contentMode = .ScaleAspectFill
imageView.clipsToBounds = true
墨落成白 2024-11-22 21:10:22
UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
UIImage *image = [UIImage imageNamed:@"myImage"];
    [image drawInRect: destinationRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文