iOS Retina显示屏蔽错误

时间:2011-02-21 00:34:39

标签: iphone ios4 cgimage retina-display

我目前正在使用两张图片作为我制作的菜单。前一段时间我正在使用这个代码用于普通的显示系统,它工作得很好,视网膜显示器我在CGImageRef上有一些问题,在背景显示器的凹陷上创建正确的蒙版图像。我尝试使用图像扩展名导入视网膜图像。图像使用以下方式提供:

[UIImage imageNamed:@"filename.png"]

我提供了带有filename.png和filename@2x.png名称的标准和视网膜图像。

选择所选区域的遮罩时会出现问题。代码适用于较低分辨率的资源和高分辨率的主资源,但是当我使用

CGImageCreateWithImageInRect

并指定我要在其中创建图像的矩形,图像的比例增加意味着主按钮的分辨率很好,但返回并叠加在按钮按下的图像不是正确的分辨率,但奇怪的是缩放到像素密度的两倍,看起来很糟糕。

我试过了两次

    UIImage *img2 = [UIImage imageWithCGImage:cgImg scale:[img scale] orientation:[img imageOrientation]];
    UIImage *scaledImage = [UIImage imageWithCGImage:[img2 CGImage] scale:4.0 orientation:UIImageOrientationUp];

当我拍摄图像和drawInRect时,我似乎无处可去:(选定的矩形)

我现在已经把头发撕掉了大约2个小时,似乎找不到合适的解决方案,有没有人有任何想法?

1 个答案:

答案 0 :(得分:16)

我想出了在这个例子中需要做些什么。我创建了一个辅助方法,在构建按下的状态图像时将图像的比例考虑在内,并使其按照图像比例缩放CGRect

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
rect.size.height = rect.size.height * [image scale];
rect.size.width = rect.size.width * [image scale];
rect.origin.x = rect.origin.x * [image scale];
rect.origin.y = rect.origin.y * [image scale];
CGImageRef sourceImageRef = [image CGImage];
CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:[image scale] orientation:[image imageOrientation]];
CGImageRelease(newImageRef);
return newImage;
}

这应该可以解决任何有类似问题的人。

相关问题