ios - UIView 中的损失质量图像

标签 ios objective-c image uiview

我的应用程序中的图片有问题。在我的滑出式菜单中,我有一个标题,我在其中放置了一个图像“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/

相关文章:

ios - 添加 xib 作为 View 以在编辑模式下覆盖自定义 UITableViewCell

objective-c - 类扩展中@property(非原子,只读)和@property 之间的区别?

ios - 如何在 Swift 中将 AnyObject 类型转换为 Int

iphone - 如何将字符串值传递给以前的 viewController

objective-c - 序列化时 JSONKit 无法识别选择器

objective-c - 如何处理在 Obj C 或 Swift 中返回对象或 nil 的 Core Data 自定义方法

java - 如何制作在调整 JFrame 大小时会调整大小的图像?

PHP-SQL : Uploaded image displaying as junk text

MATLAB 中的图像上采样使用 image() 和 imshow() 生成白色图像

ios - iOS从NSArray获取对象