iOS Retina显示屏遮罩漏洞

3

我目前在使用两张图片来构建一个菜单。以前我在普通的显示系统上使用这段代码时,它能够正常工作,但是在视网膜显示屏上,CGImageRef在背景显示压下状态时创建正确的遮罩图像存在问题。我尝试使用视网膜图片的图像扩展进行导入。这些图像是使用以下方式提供的:

[UIImage imageNamed:@"filename.png"]

我提供了标准和Retina图像,文件名分别为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)方法时,似乎无法取得进展。

我已经抓狂了大约两个小时,但好像找不到一个合适的解决方案,有人有什么想法吗?

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;
}
那应该能解决任何遇到类似映射问题的人的困扰。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接