【问题标题】:Retina display of images-iPhone 3 to 4图像的视网膜显示-iPhone 3 到 4
【发布时间】:2011-01-15 08:29:21
【问题描述】:

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

它适用于所有 iPhone,但现在我希望它也适用于 Retina 显示器。

因此,按照this link,我拍摄了另一张图片,其尺寸是当前图片尺寸的两倍,并通过添加后缀@2x 对其进行重命名。但问题是它只占用了视网膜显示图像的上半部分。我认为那是因为我在使用CGImageCreateWithImageInRect 时设置的框架。那么为了完成这项工作应该做些什么。

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

提前谢谢...

【问题讨论】:

    标签: iphone image retina-display


    【解决方案1】:

    问题可能是 @2x 图像比例仅针对 UIImage 的某些初始化程序自动正确设置...Try loading your UIImages using code like this from Tasty Pixel. 该链接中的条目详细讨论了此问题。

    使用链接中的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);

    【讨论】:

    • 我也试过了,但它没有得到规模==2.0。我在模拟器里试过了。
    • 也没有实现这个它正在以命名为@2x 作为后缀的图像。但是现在由于上述条件不匹配它根本没有拍摄我的@2x 图像
    • 我编辑了我的答案 - 如果它不起作用,请发布一些代码,我会尽力提供帮助。
    • 我做了同样的事情..它需要@2x图像..但它仍然只剪切图像的上半部分并省略下半部分。
    【解决方案2】:

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

    //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] 属性来确定您应该切片多大的矩形。

    【讨论】:

    • 所以你想说如果我在 iPhone 3 中拍摄 100x100 的切片用于视网膜显示屏,我需要拍摄 200x200 的切片吗?是这样吗?
    • 尝试了你的代码,但它使图块变大,因此导致图块超出屏幕:(
    • @hAPPY iCODER - 哦,不。这是为了对 UIImage 进行切片,无论是 myImage.png 还是 myImage@2x.png。
    • 我已经编写了切片代码。但我需要适合屏幕的 2x 图像切片,就好像它适合普通图像一样。现在切片 2x 会使图块更大走出屏幕
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多