0

CGImageCreateWithImageInRect ( originalImage.CGImage, frame );iPhone 3用のタイルゲームのアプリケーションを開発しました。リソースから画像を取得し、関数を使用してタイルの数に分割しました。

すべてのiPhoneでうまく機能しますが、Retinaディスプレイでも機能するようにしたいと思います。

したがって、このリンクに従って、現在の画像サイズの2倍のサイズの画像を取得し、接尾辞@2xを追加して名前を変更しました。ただし、問題は、網膜表示画像の上半分のみを取得することです。使用中に設定したフレームのせいだと思いますCGImageCreateWithImageInRect。それで、この仕事をすることに関して何がなされるべきか。

どんな種類の助けも本当にありがたいです。

前もって感謝します...

4

2 に答える 2

1

問題は、@2x画像スケールがUIImageの特定の初期化子に対してのみ自動的に適切に設定される可能性があります... TastyPixelからこのようなコードを使用して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);

于 2011-01-15T08:55:50.237 に答える
0

私が行ったアプリで動作させる方法は次のとおりです。

//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]プロパティを使用して、スライスする必要がある四角形の大きさを把握することです。

于 2011-01-15T18:30:39.427 に答える