原文转自:http://atastypixel.com/blog/uiimage-resolution-independence-and-the-iphone-4s-retina-display/
iOS4 caters for the high-resolution Retina display that comes with the iPhone 4 by some rather clever abstraction, that moves away from the concept of ‘pixels’, and instead uses ‘points’, which are resolution-independent.
So, when you display an image that’s been prepared for the Retina display, it’s represented with a scale factor of 2, meaning that to your code, it appears to have the same dimensions, but in fact contains twice the information density.
iOS4′s UIImage makes it work by automatically looking for high-res images located alongside the prior ‘standard resolution’ ones — identified by a “@2x” suffix to the filename.
This works great with +[UIImage imageNamed:]
, but although the API documentation says that other image loading methods will automatically load the @2x versions, they actually don’t. Yeah. Apple are working on it.
Until they sort themselves out, I’m using a convenience method sitting inside a UIImage category. So, where I would previously use something like [UIImage imageWithContentsOfFile:]
, I now use [UIImage imageWithContentsOfResolutionIndependentFile:]
.
Here’s the category:
@interface UIImage (TPAdditions)
- (id)initWithContentsOfResolutionIndependentFile:(NSString *)path;
+ (UIImage*)imageWithContentsOfResolutionIndependentFile:(NSString *)path;
@end
@implementation UIImage (TPAdditions)
- (id)initWithContentsOfResolutionIndependentFile:(NSString *)path {
if ( [[[UIDevice currentDevice] systemVersion] intValue] >= 4 && [[UIScreen mainScreen] scale] == 2.0 ) {
NSString *path2x = [[path stringByDeletingLastPathComponent]
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@@2x.%@",
[[path lastPathComponent] stringByDeletingPathExtension],
[path pathExtension]]];
if ( [[NSFileManager defaultManager] fileExistsAtPath:path2x] ) {
return [self initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:path2x]] CGImage] scale:2.0 orientation:UIImageOrientationUp];
}
}
return [self initWithData:[NSData dataWithContentsOfFile:path]];
}
+ (UIImage*)imageWithContentsOfResolutionIndependentFile:(NSString *)path {
return [[[UIImage alloc] initWithContentsOfResolutionIndependentFile:path] autorelease];
}
@end
This checks to see whether the iOS4 features are present, so this code should work on devices running prior OS versions as well.
What iOS4′s imageWithContentsOfFile
does do is recognise if an image is the ’2x’ version, and sets the scale
accordingly, so it displays correctly.
Update: I just discovered that -[UIDevice scale]
actually exists prior to iOS 4. Apple recommend using tests like respondsToSelector
to determine OS features, rather than version checks, but in this case, it gives the wrong result that can result in some pretty hard to find bugs. I’ve updated the above class to check the iOS version instead.
I’ve also avoided using initWithContentsOfFile
entirely: As Tea mentioned in a comment below, we probably shouldn’t be trusting this method any more, as it does not behave as advertised.
Update 2: I’ve been unable to replicate the problem, but a few commenters below noted issues with the 2x image being displayed at double-size. I’ve now updated the above to explicitly set the scale to 2.0 for the 2x image, which should fix the problem.
分享到:
相关推荐
在iPhone应用中,由于屏幕尺寸和性能考虑,经常需要对UIImage对象进行尺寸调整,以确保图片在不同设备上正确显示和高效加载。下面将详细介绍标题和描述中提及的"UIImage resize"知识点,以及如何通过自定义类别来...
Overwrites UIImageView & UIImage's description to print out the image in ASCII Art.zip,Overwrites UIImageView & UIImage's description to print out the image in ASCII Art
在iOS开发中,`UIImage`类是用于处理和显示图像的核心类,但默认情况下它并不支持PDF文档的显示。为了扩展`UIImage`的功能,开发者通常会创建分类(Category)来添加新的方法。`UIImage-PDF`就是这样一个Objective-C...
标题“Post a UIImage to the web”暗示了这个话题主要涉及如何在iOS开发中将UIImage对象上传到网络。UIImage是Apple的iOS SDK中的一个类,用于处理和显示图像。在这个过程中,可能涉及到网络请求、数据编码以及与...
在iOS开发中,`UIImage`是苹果提供的一个关键类,用于处理和显示图像。这个类扩展了`UIImage`,增加了精灵(Sprite)和动画功能,让开发者能够更灵活地创建和控制游戏或应用中的动态图像。`UIImage+Sprite+Additions...
OC Extension UIImage+FHXImage(图片扩展) 针对UIImage进行封装的工具类。 /** 输入图片颜色返回一张图片 */ + (UIImage *)createImageWithColor:(UIColor *)color; /** 裁切图片的一个点进行延伸 */ - ...
4. **遮罩应用**:除了单纯的模糊,`UIImage+ImageEffects`可能还支持将遮罩应用到图片上,使得模糊只在特定区域内生效,增加设计的层次感。 5. **自定义滤镜**:除了模糊,这个类可能还包括其他Core Image滤镜,如...
在iOS开发中,UIImage是苹果提供的一个关键类,用于显示和处理图像。当你需要对图片进行颜色调整或添加遮罩效果时,可以利用UIKit框架中的各种方法和属性来实现。下面将详细介绍如何改变UIImage的颜色以及如何应用...
这个“WebP-UIImage-源码.rar”压缩包可能包含了用Objective-C或Swift实现的iOS平台上对WebP图像格式的支持,使得开发者可以直接在UIImage对象中加载和显示WebP图片。 在iOS开发中,UIImage是苹果提供的一个关键类...
在iOS开发中,UIImage是苹果提供的一个核心类,用于处理和显示图像。这个"UIImage+Sprite for iOS"的源码库显然扩展了UIImage类,增加了精灵(Sprite)功能,这通常用于2D游戏或者需要高效处理多帧动画的场景。在iOS...
ImageHelper可与iPhone 4和Retina显示器配合使用,并使用正确的比例因子和高分辨率图像。 基本示例用法显示了在格式之间来回转换的能力: //查看示例项目的实际用法 NSString *path = (NSString*)[[NSBundle ...
在Swift编程语言中,对UIView或UIImage进行高性能的圆角处理和生成渐变色图片是常见的图像操作。这些操作在用户界面设计中尤其重要,因为它们可以提升应用的视觉效果和用户体验。以下将详细介绍如何实现这些功能。 ...
3. 采样率优化:对于高分辨率的图片,可以降低其色彩采样率(例如从4:4:4降低到4:2:0),这会减少颜色信息,但对视觉效果的影响较小。 4. 进行适当的色彩空间转换:根据目标平台和应用场景,选择合适的色彩空间,如...
UIImage Category是一种优雅的方式,可以扩展UIImage的功能,无需修改原生类,方便地实现水印功能。本教程将深入讲解如何通过Category实现UIImage的水印功能。 首先,让我们了解Category的概念。Category是...
来源:github/UIImage-HelpersLicence:MIT作者:No Zebra Network 提供三个UIImage的category,能够让UIImage支持截屏、模糊以及根据颜色生成图片的功能。
来源:github/UIImage-PDFLicence:Custom作者:Nigel Timothy Barber UIImage的一个 category (UIImage+PDF),能够让 UIImage 也能显示pdf格式的文件,这样可以方便利用 UIImageView 的缩放函数来对pdf图像...
在iOS开发中,录制视频流并将其转换为UIImage是一项常见的需求,这通常涉及到多媒体处理、图形编程以及实时渲染等技术。下面将详细讲解这个过程涉及的知识点。 首先,我们需要了解如何在iOS上进行实时视频录制。...
UIImage-BlurredFrame, UIImage类别,模糊了UIImage的指定框架 UIImage+BlurredFrame是一个UIImage类别,模糊了UIImage的指定框架#Install使用 cocoapods 安装pod 'UIImage+BlurredFrame'#Usage