3

我目前正在为我构建的菜单使用两个图像。不久前,我将这段代码用于普通显示系统,它工作正常,在视网膜显示器上我遇到了一些问题,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:(Selected Rect) 时,我似乎无处可去

我现在已经把头发扯了大约 2 个小时,似乎找不到一个像样的解决方案,有人有什么想法吗?

4

1 回答 1

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;
}

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

于 2011-02-21T01:35:21.227 回答