我目前正在使用两个图像作为我构建的菜单。我不久前在普通显示系统上使用了这段代码,它工作得很好,在视网膜显示上,我在 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)时,我似乎一无所获
我已经折腾了大约两个小时,似乎找不到合适的解决方案,有人有什么想法吗?
最佳答案
我想出了在这种情况下需要做什么。我创建了一个辅助方法,在构建按下状态图像时会考虑图像的比例,并使其按图像比例缩放 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;
}
这应该可以解决任何有类似映射问题的人。
关于iphone - iOS 视网膜显示遮蔽错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5061222/