我的应用程序中的图片有问题。在我的滑出式菜单中,我有一个标题,我在其中放置了一个图像“header.png”,它存在两个版本“header.png”和“header@2x.png”。这是我如何在我的应用程序中实现它的代码:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIImage *originalImage = [UIImage imageNamed:@"header.png"];
CGSize destinationSize = CGSizeMake(320, 150);
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *headerView = [[UIImageView alloc] initWithImage:newImage];
headerView.frame = CGRectMake(0,0, 320,150);
return headerView;
}
当我在我的手机 (iPhone 4s) 上运行该应用程序时,我的图像像素化,边缘被吹散并且不是很干净......我不知道它来自哪里。
我的图片是 320x150px 和 640x300px,72dpi
谢谢
--编辑--
我已经使用 UIImageView 解决了我的问题,如下所示:
- (UIImageView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString* fileName = [NSString stringWithFormat:@"header.png"];
UIImage *newImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]];
UIImageView *headerView = [[UIImageView alloc] initWithImage:newImage];
headerView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 150);
return headerView;
}
最佳答案
问题是 UIGraphicsBeginImageContext
不会为您提供视网膜图像,除非您使用 UIGraphicsBeginImageContextWithOptions(destinationSize, NO, scale)
,其中 scale
可能类似于 [UIScreen mainScreen].scale
。
只是出于好奇:你为什么不简单地使用
UIImage *newImage = [UIImage imageNamed:@"header.png"]
return [[UIImageView alloc] initWithImage:newImage];
关于ios - UIView 中的损失质量图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24331092/