High-Res UIImage remote load

Loading UIImages automatically in high-resolution works fine for locally stored images – but if you want to fetch them via remote URL you have to code yourself.

A simple, blocking but backward compatible (iOS >= 3.0, maybe even 2.0 but untested)  implementation could look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@implementation UIImage (MRORemote)
 
// add the @2x filename suffix
+(NSURL *)url2x:(NSURL *)url
{
  NSString *path = url.path;
  NSAssert(path != nil, @"");
  NSString *last = path.lastPathComponent;
  NSString *ext = path.pathExtension;
  NSAssert(last != nil, @"");
  NSAssert(ext != nil, @"");
  NSString *part = [last substringToIndex:MAX(0, last.length - ext.length - 1)];
  return [NSURL URLWithString:[NSString stringWithFormat:@"%@@2x.%@", part, ext] relativeToURL:[url absoluteURL]];
}
 
+(UIImage *)imageWithContentsOfURL:(NSURL *)url probe2x:(BOOL)probe2x
{
  if ( url == nil )
    return nil;
  UIScreen *screen = [UIScreen mainScreen];
  const CGFloat scale = [screen respondsToSelector:@selector(scale)] ? [screen scale] : 1.0f;
  if ( probe2x && 2.0f == scale && [UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)] ) {
    UIImage *img = nil;
    NSData *raw = [NSData dataWithContentsOfURL:[UIImage url2x:url]];
    if ( raw != nil )
      img = [UIImage imageWithData:raw];
    if ( img != nil )
      img = [UIImage imageWithCGImage:img.CGImage scale:scale orientation:img.imageOrientation];
    if ( img != nil )
      return img;
    NSAssert(raw == nil && img == nil, @"");
  }
  // MRLogD(@"loading %@", [url absoluteURL]);
  NSData *raw = [NSData dataWithContentsOfURL:url];
  if ( raw == nil )
    return nil;
  return [UIImage imageWithData:raw];
}
@end

Use at your will but without any warranty.

Flattr this!

Post a Comment

Your email is never published nor shared. Required fields are marked *