2

默认情况下,UIImageView 在 UIImageView 中将他的图像显示为 1px of image = 1pt,但我想显示为 2px of image = 1pt。

使用名称“..@2x..”保存图像的版本不合适,图像未保存在文件系统中。

比如图片大小是400x100,我想把图片显示在显示器的中央,应该是图片左边120pt,右边120pt(640-400) / 2

4

2 回答 2

1

这很容易,请注意对于 iOS 4 以下的版本,您没有 Retina 显示器,这就是为什么在图像缩放方法中我首先进行此检查:

//Retina detect
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){

然后image从某个地方(文件,缓存等)加载,我正在以这种方式缩放它

UIImage * image2Xscaled = [UIImage alloc];
            image = [[image2Xscaled initWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp] autorelease];

方法

initWithCGImage:scale:orientation:

在 iOS 4.0 中可用。这就是为什么你需要第一次检查。如果不支持销售,则返回 1.0 缩放图像。

于 2012-07-16T09:39:42.197 回答
0

你可以使用这样的东西:

NSData *data = //your data, however you get that...
UIImage *image = [UIImage imageWithData:data];
if ([UIScreen mainScreen].scale > 1.0f)
{
    image = [UIImage imageWithCGImage:image
                                scale:[UIScreen mainScreen].scale
                          orientation:UIImageOrientationUp]
}
于 2012-07-16T09:38:53.737 回答