【发布时间】:2009-08-09 04:56:19
【问题描述】:
我对 UIImageView 有一个非常奇怪的问题。我有一个 45x45 像素的图像(RGB png),我添加到视图中。我可以看到图像在添加到视图后变得模糊。这是模拟器(左)和 Xcode(右)中的相同图像:
(来源:partywithvika.com)
(来源:partywithvika.com)
我有这个 initWithImage 代码的自定义 UIImageView 类:
- (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];
为什么显示的图像与存储在文件中的图像不同?
【问题讨论】:
-
这可能与问题无关,但是如果在分配
self = [super initWithImage:image]后检查 self 是否为 nil,如果是,则立即返回 nil,您的 init 方法会更传统。如果 self 为 nil,则要么是因为 (1) 父级的初始化程序无法正确初始化自身,要么 (2) 由于某些奇怪的内存条件(非常罕见),alloc 返回了 nil。这是一种很好的防御性编程实践。 -
感谢您的留言,我会试试这个。
标签: objective-c iphone uiimageview blur