Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>imageWithData:</code> method will always create an image with a scale of 1.0 (non-retina). Creating a retina-aware UIImage from a custom location outside the bundle, you need to use the <code>initWithCGImage:scale:orientation:</code> method:</p> <pre><code>UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.test.com/img2@2x.png"]]]; UIImage *retinaImage = [UIImage initWithCGImage:[image CGImage] scale:2.0 orientation:UIImageOrientationUp]; </code></pre> <p>(and, obviously, you shouldn't download the image synchronously...).</p> <p>I needed to do the same thing (loading scale-dependent images from the documents directory) in my last project, so I've written a small convenience method in a UIImage category:</p> <pre><code>- (id)initWithContentsOfResolutionIndependentFile:(NSString *)path { if([[UIScreen mainScreen] scale] == 2.0) { NSString *path2x = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@@2x.%@", [[path lastPathComponent] stringByDeletingPathExtension], [path pathExtension]]]; if([[NSFileManager defaultManager] fileExistsAtPath:path2x]) { return [self initWithCGImage:[[UIImage imageWithData:[NSData dataWithContentsOfFile:path2x]] CGImage] scale:2.0 orientation:UIImageOrientationUp]; } } return [self initWithContentsOfFile:path]; } </code></pre> <p>Usage:</p> <pre><code>UIImage *myImage = [[UIImage alloc] initWithContentsOfResolutionIndependentFile:@"/path/to/image.png"]; </code></pre> <p>This will try to load the image from <code>/path/to/image@2x.png</code> when on retina, and use <code>/path/to/image.png</code> otherwise.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload