图像的视网膜显示 - iPhone 3 至 4

发布于 2024-10-12 13:52:44 字数 504 浏览 5 评论 0原文

我为iPhone 3开发了一款瓷砖游戏应用程序。 其中,我从资源中获取图像,并使用 CGImageCreateWithImageInRect (originalImage.CGImage, frame); 函数将其划分为多个图块。

它在所有 iPhone 上都能很好地工作,但现在我希望它也能在 Retina 显示屏上工作。

因此,根据这个link 我拍摄了另一张图像,其大小是当前图像大小的两倍,并通过添加后缀@2x对其进行重命名。但问题是它只占用视网膜显示图像的上半部分。我认为这是因为我在使用 CGImageCreateWithImageInRect 时设置的框架。那么要做什么才能使这项工作发挥作用呢?

任何形式的帮助将不胜感激。

提前致谢...

I have developed an application of tile game for iPhone 3.
In which I took an image from my resource and divided it into number of tiles using CGImageCreateWithImageInRect ( originalImage.CGImage, frame ); function.

It works great on all iPhones but now I want it to work on Retina Displays also.

So as per this link I have taken anothe image with its size double the current images size and rename it by adding suffix @2x. But the problem is it takes the upper half part of the retina display image only. I think thats because of the frame I have set while using CGImageCreateWithImageInRect. So What shall be done in respect to make this work.

Any kind of help will be really appreciated.

Thanks in advance...

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

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

发布评论

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

评论(2

童话 2024-10-19 13:52:44

问题可能是 @2x 图像比例仅针对 UIImage 的某些初始值设定项自动正确设置... 尝试使用来自 Tasty Pixel 的代码加载 UIImages。 该链接中的条目更多地讨论了这个问题。

使用链接中的 UIImage+TPAdditions 类别,您将像这样实现它(在确保图像及其 @2x 对应项位于您的项目中之后):

NSString *baseImagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *myImagePath = [baseImagePath stringByAppendingPathComponent:@"myImage.png"]; // note no need to add @2x.png here
UIImage *myImage = [UIImage imageWithContentsOfResolutionIndependentFile:myImagePath];

然后您应该能够使用 <代码>CGImageCreateWithImageInRect(myImage.CGImage,frame);

The problem is likely that the @2x image scale is only automatically set up properly for certain initializers of UIImage... Try loading your UIImages using code like this from Tasty Pixel. The entry at that link talks more about this issue.

Using the UIImage+TPAdditions category from the link, you'll implement it like so (after making sure that the images and their @2x counterparts are in your project):

NSString *baseImagePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *myImagePath = [baseImagePath stringByAppendingPathComponent:@"myImage.png"]; // note no need to add @2x.png here
UIImage *myImage = [UIImage imageWithContentsOfResolutionIndependentFile:myImagePath];

Then you should be able to use CGImageCreateWithImageInRect(myImage.CGImage, frame);

泅人 2024-10-19 13:52:44

以下是我如何让它在我所做的应用程序中工作:

//this is a method that takes a UIImage and slices it into 16 tiles (GridSize * GridSize)
#define GridSize 4
- (void) sliceImage:(UIImage *)image {
    CGSize imageSize = [image size];
    CGSize square = CGSizeMake(imageSize.width/GridSize, imageSize.height/GridSize);

    CGFloat scaleMultiplier = [image scale];

    square.width *= scaleMultiplier;
    square.height *= scaleMultiplier;

    CGFloat scale = ([self frame].size.width/GridSize)/square.width;

    CGImageRef source = [image CGImage];
    if (source != NULL) {
        for (int r = 0; r < GridSize; ++r) {
            for (int c = 0; c < GridSize; ++c) {
                CGRect slice = CGRectMake(c*square.width, r*square.height, square.width, square.height);
                CGImageRef sliceImage = CGImageCreateWithImageInRect(source, slice);
                if (sliceImage) {
                    //we have a tile (as a CGImageRef) from the source image
                    //do something with it
                    CFRelease(sliceImage);
                }
            }
        }
    }
}

技巧是使用 -[UIImage scale] 属性来确定应该切片多大的矩形。

Here's how I got it to work in an app I did:

//this is a method that takes a UIImage and slices it into 16 tiles (GridSize * GridSize)
#define GridSize 4
- (void) sliceImage:(UIImage *)image {
    CGSize imageSize = [image size];
    CGSize square = CGSizeMake(imageSize.width/GridSize, imageSize.height/GridSize);

    CGFloat scaleMultiplier = [image scale];

    square.width *= scaleMultiplier;
    square.height *= scaleMultiplier;

    CGFloat scale = ([self frame].size.width/GridSize)/square.width;

    CGImageRef source = [image CGImage];
    if (source != NULL) {
        for (int r = 0; r < GridSize; ++r) {
            for (int c = 0; c < GridSize; ++c) {
                CGRect slice = CGRectMake(c*square.width, r*square.height, square.width, square.height);
                CGImageRef sliceImage = CGImageCreateWithImageInRect(source, slice);
                if (sliceImage) {
                    //we have a tile (as a CGImageRef) from the source image
                    //do something with it
                    CFRelease(sliceImage);
                }
            }
        }
    }
}

The trick is using the -[UIImage scale] property to figure out how big of a rect you should be slicing.

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