gpt4 book ai didi

objective-c - UIImageView 模糊图像

转载 作者:太空狗 更新时间:2023-10-30 03:47:31 24 4
gpt4 key购买 nike

我对 UIImageView 有一个非常奇怪的问题。我有一个图像(RGB png)45x45 像素,我将其添加到 View 中。我可以看到图像在添加到 View 后变得模糊。这是模拟器(左)和 Xcode(右)中的相同图像:

alt text
(来源:partywithvika.com)

alt text
(来源:partywithvika.com)

我有自定义 UIImageView 类和这个 initWithImage 代码:

- (id) initWithImage:(UIImage*) image {
self = [super initWithImage:image];

self.frame = CGRectMake(0, 0, 45, 45);
self.contentMode = UIViewContentModeScaleAspectFit;

self.quantity = 1;
if (self) {
self.label = [[UITextField alloc]initWithFrame:CGRectMake(0,40,45,25)];
self.label.font = [UIFont systemFontOfSize:16];
self.label.borderStyle = UITextBorderStyleNone;
self.label.enabled = TRUE;
self.label.userInteractionEnabled = TRUE;
self.label.delegate = self;
self.label.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
self.label.textAlignment = UITextAlignmentCenter;
}
self.userInteractionEnabled = TRUE;

// Prepare 3 buttons: count up, count down, and delete
self.deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.deleteButton.hidden = NO;
self.deleteButton.userInteractionEnabled = YES;
self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
self.deleteButton.titleLabel.textColor = [UIColor redColor];
[self.deleteButton setTitle:@"X" forState:UIControlStateNormal];
[self.deleteButton addTarget:self action:@selector(deleteIcon:) forControlEvents:UIControlEventTouchUpInside];

self.upCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.upCountButton.hidden = NO;
self.upCountButton.userInteractionEnabled = YES;
[self.upCountButton setTitle:@"+" forState:UIControlStateNormal];
[self.upCountButton addTarget:self action:@selector(addQuantity:) forControlEvents:UIControlEventTouchUpInside];

self.downCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.downCountButton.hidden = YES;
self.downCountButton.userInteractionEnabled = YES;
[self.downCountButton setTitle:@"-" forState:UIControlStateNormal];
[self.downCountButton addTarget:self action:@selector(removeQuantity:) forControlEvents:UIControlEventTouchUpInside];
return self;
}

我是这样创建的:

UIImage *desertIcon = [UIImage imageNamed:@"desert.png"];
IconObj *desertIconView = [[IconObj alloc] initWithImage:desertIcon];
desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.type = [IconObj TYPE_DESERT];
[self.view addSubview:desertIconView];
[desertIconView release];

为什么显示出来的图片和保存在文件里的图片不一样?

最佳答案

一般来说,您在 iPhone 上使用 UIKit 所做的一切都应该是像素对齐的。像素对齐问题通常表现为模糊(文本和图像尤其是)。这就是为什么当我发现模糊 View 时,我首先检查我是否正在设置 center 属性。当您设置 center 时,框架的 x、y、高度和宽度会围绕该中心点进行调整...经常会产生小数值。

您可以尝试在框架上使用 CGRectIntegral,如下所示:

desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.frame = CGRectIntegral(desertIconView.frame);

这可能有效,但如果无效,请尝试手动设置 frame,而不使用 center 属性以确保没有值是小数。

编辑:糟糕!没有注意到 OP 已经回答了他自己的问题......无论如何我会出于信息原因将其保留。

关于objective-c - UIImageView 模糊图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1250674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com