Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High Resolution images in a uiwebview

Tags:

html

uiwebview

I have a webview that displays an image, as shown in the code below. The bundle also has a [email protected] with dimensions of 128x128 for use on the iPhone4. The [email protected] never shows. Is there a way to display either/or depending on whether it's an iPhone or an iPhone4?

<img src="DGT64.png" width="64" height="64" align="left" style="padding:2px;"/>
like image 887
Matt Winters Avatar asked Sep 06 '10 16:09

Matt Winters


Video Answer


2 Answers

The way to do it is with CSS backgrounds. Just embedd all your 2x stuff in a subsection in CSS. The key is also to set the -webkit-background-size. Here is an example of the portion of the css (both retina and not) for a div with the id Ampersand that acts as an image.

div#Ampersand {
  background: url('AmpersandBurned.png');
  width:43px;
  height:97px;
  float:left;
  -webkit-background-size: 43px 97px;
}

@media screen and (-webkit-device-pixel-ratio: 2) {
  div#Ampersand {
    background: url('[email protected]');
    width:43px;
    height:97px;
    float:left;
  }
}
like image 73
monkeydom Avatar answered Nov 05 '22 06:11

monkeydom


I don't think the @2x trick works with web content. Sounds really useful though, I would certainly file a bug with Apple to request that.

If you are generating the above HTML from your app then I think the best way for now will be to detect if you are running on a Retina display device and then adjusting the image name manually.

You can detect the Retina display by doing something like this:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[UIScreen mainScreen] scale] == 2) {
        // Use high resolution images
    }
}

(Took that code from an answer I found here in SO)

like image 30
Stefan Arentz Avatar answered Nov 05 '22 07:11

Stefan Arentz