iOS 视网膜显示遮蔽错误

发布于 2024-10-18 11:04:29 字数 946 浏览 7 评论 0原文

我目前正在使用两个图像作为我构建的菜单。我不久前在普通显示系统上使用了这段代码,它工作得很好,但在视网膜显示器上,我在 CGImageRef 在背景显示的凹陷上创建正确的蒙版图像时遇到了一些问题。我尝试使用视网膜图像的图像扩展进行导入。图像是使用以下方式提供的:

[UIImage imageNamed:@"filename.png"]

我提供了标准图像和视网膜图像,其中包含 filename.png 和 [电子邮件受保护] 名称。

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

CGImageCreateWithImageInRect

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

我都尝试过

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

,当我拍摄图像和drawInRect:(选定的矩形)时,我似乎一无所获,

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

I'm currently using two images for a menu that I've built. I was using this code a while ago for normal display systems and it was working fine, with the retina display I'm having some issues with CGImageRef creating the right masked image on a depress for the background display. I've tried importing using the Image extensions for retina images. The images are supplied using:

[UIImage imageNamed:@"filename.png"]

I've provided both a standard and a retina image with both the filename.png and the [email protected] names.

The problem comes when choosing the mask for the selected area. The code works fine with lower resolution resources, and a high resolution main resource, but when I use

CGImageCreateWithImageInRect

And specify the rect that I want to create the image within, the image's scale is increased meaning that the main button's resolution is fine, but the image that is returned and superimposed on the button downpress is not the correct resolution, but oddly scaled to twice the pixel density, which looks terrible.

I've tried both

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

And I seem to be getting nowhere when I take the image and drawInRect:(Selected Rect)

I have been tearing my hair out for about 2 hours now, and can't seem to find a decent solution, does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2024-10-25 11:04:29

我想出了在这种情况下需要做什么。我创建了一个辅助方法,在构建按下状态图像时会考虑图像的比例,并使其按图像比例缩放 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;
}

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

I figured out what is necessary to be done in this instance. I created a helper method that would take the scale of the image into account when building the pressed state image and made it scale the CGRect by the image scale like so

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

That should fix anyone having similar issues for mapping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文