23

私のアプリケーションでは、ウェブから(正確にはサーバーから)いくつかの画像をダウンロードしています。帯域幅と特に電話のメモリを節約するために、「古い」iPhoneシリーズ用の480x320とiPhone 4 Retina ディスプレイの場合は 960x640。次に、Retina スクリーンをサポートするデバイスでアプリが実行されていることをアプリ内から検出できるようにする必要があります。どうすればそれができますか?

以下のコード スニペットを使用して、特定のデバイス識別子を返すことを考えていました。「iPhone3」ですが、検出を iPhone4 に限定し、Retina ディスプレイを備えた後続のデバイス用にそのコードを更新する必要があります。

size_t size;

// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0); 

// Allocate the space to store name
char *name = malloc(size);

// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);

// Place name into a string
NSString *machine = [NSString stringWithCString:name];

より良い解決策はありますか (おそらくそれは非常に明白ですが、私はそれを見逃しました)?

4

11 に答える 11

32

公式の Apple Developers Forums を読んだところ、問題はそこで長々と議論されています。私にとって最良の方法は、 のscaleプロパティを使用することですUIScreen。以降でしか利用できiOS 4ませんが、知っておく必要があるすべてのことを教えてくれ、将来さらに重要な役割を果たす可能性が高いです (iPad の画面解像度 1024x768 が正確に 32/15 * 480x320 であることはすでにお気づきですか?) .

UIScreen.mainScreen.scale

さらに別のアイデアがある場合は、お気軽に投稿してください:)

于 2010-07-17T19:05:46.800 に答える
23

iOS 3.x と 4.x の両方で正しい方法で行うためのコードを次に示します。

BOOL hasHighResScreen = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
    CGFloat scale = [[UIScreen mainScreen] scale];
    if (scale > 1.0) {
        hasHighResScreen = YES;
    }
}
于 2010-08-28T02:36:34.093 に答える
13

Scott Gustafson による回答の更新:

iPad/Retina/iphone を区別する必要がある場合:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        CGFloat scale = [[UIScreen mainScreen] scale];

           if (scale > 1.0) 
           {
                //iPad retina screen
           }  
           else
           {
                //iPad screen
           }
    } 
    else
    {
         if ([UIScreen instancesRespondToSelector:@selector(scale)]) 
         {
               CGFloat scale = [[UIScreen mainScreen] scale];

               if (scale > 1.0) 
               {
                    if([[ UIScreen mainScreen ] bounds ].size.height == 568)
                    {
                        //iphone 5
                    }
                    else
                    {
                        //iphone retina screen
                    }
               }
               else
               {
                    //iphone screen
               }
         }
    }
于 2011-09-30T07:12:09.640 に答える
5

この方法で実際の画面サイズ (ピクセル単位) を取得します。

UIScreen *MainScreen = [UIScreen mainScreen];
UIScreenMode *ScreenMode = [MainScreen currentMode];
CGSize Size = [ScreenMode size]; // <--- Real screen size
于 2010-11-06T17:51:07.190 に答える
3
- (BOOL)isRetina {

    BOOL isRetina = NO;

    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
        CGFloat scale = [[UIScreen mainScreen] scale];
        if (scale > 1.0) {
            isRetina = YES;
        }
    }

    return isRetina;
}


- (CGSize)sizeInPixels {

    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
    CGSize screenSize = CGSizeMake(appFrame.size.width, appFrame.size.height);

    return [self isRetina] ? CGSizeMake(screenSize.width * 2, screenSize.height * 2) : screenSize;
}
于 2012-07-05T13:49:56.467 に答える
1

ロビンの答えに行きます。もう 1 つ注意: デバイス名を確認する必要がある場合は、UIDevice のメソッドを使用してください。

[[UIDevice currentDevice] model];
[[UIDevice currentDevice] systemName];
[[UIDevice currentDevice] systemVersion];
于 2010-08-14T22:13:32.630 に答える
1

そして、iphone/iphone_retina/ipad/ipad_retina を検出する方法をコピー/貼り付けしたいだけの人のために、このスレッドを読んだ後に私がやったことは次のとおりです。Guntis Treulands の貢献に大いに触発され、Guntis Treulands は Scott Gustafson の回答を拡張しました。

- (NSString *) yesButWhichDeviceIsIt {    
    BOOL hasRetina = NO;
    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
        CGFloat scale = [[UIScreen mainScreen] scale];
        if (scale > 1.0) {
            hasRetina = YES;
        }
    }
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (hasRetina) {
            return @"iPad retina";
        } else {
            return @"iPad";
        }
    } else {
        if (hasRetina) {
            return @"iPhone retina";
        } else {
            return @"iPhone";
        }        
    }
}
于 2012-05-07T19:01:15.453 に答える
1
UIScreen *MainScreen = [UIScreen mainScreen];
UIScreenMode *ScreenMode = [MainScreen currentMode];
CGSize Size = [ScreenMode size]; // <--- Real screen size
于 2010-11-26T11:34:50.620 に答える
0
+(BOOL)Retina{
        return ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))?1:0; }
于 2012-05-23T11:03:43.680 に答える
0

Cocos2d を使用している場合は、次のことを試してください。

[[CCDirector sharedDirector] winSizeInPixels];

CGSizeプロパティwidthとを含む を返しますheight

于 2011-06-15T01:45:06.477 に答える
-1

すでに回答を選択していますが、特に画像を扱う場合はもっと簡単な方法があるので、それについても言及します。

バンドルに 2 つのサイズ (320x480 と 640x960) の 2 つの画像を含め、後者の画像のファイル名の末尾に「@2x」を追加すると、[UIImage imageNamed:] は自動的に古いデバイス用に小さい画像を選択し、2x Retina ディスプレイを搭載したデバイスの場合は、画像の接尾辞を省略します。元。:

@"image.png" と @"image@2x.png" という名前の 2 つの画像。どちらもアプリ バンドルに含まれています。

次に呼び出します。

[UIImage imageNamed:@"image"];

これは、アプリのアイコンとロード画面の仕組みでもあります。

于 2011-12-20T04:40:56.473 に答える