Retina image resources? #12

Open
grgcombs opened this Issue Jul 17, 2011 · 2 comments

Projects

None yet

3 participants

@grgcombs

Any advice or information on how to use this to handle loading up retina (@2x) vs. standard resolution images?

I solved this by replacing the following in EGOImageLoader:

- (void)imageLoadConnectionDidFinishLoading:(EGOImageLoadConnection *)connection {
    UIImage* anImage = [UIImage imageWithData:connection.responseData];
    ...

with:

- (void)imageLoadConnectionDidFinishLoading:(EGOImageLoadConnection *)connection {
    CFDataRef imageData = (CFDataRef)connection.responseData;
    CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData(imageData);
    CGImageRef image = CGImageCreateWithPNGDataProvider(imageDataProvider, NULL, true, kCGRenderingIntentDefault);

    UIImage* anImage = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
    CGDataProviderRelease(imageDataProvider);
    CGImageRelease(image);
    ...

Works for PNGs. Replace CGImageCreateWithPNGDataProvider if you need something else.

rnaud commented May 13, 2012

Two things in there : You can actually try for CGIImageCreatWithPNG and use JPEG if it failed like so :

- (void)imageLoadConnectionDidFinishLoading:(EGOImageLoadConnection *)connection {
  CFDataRef imageData = (CFDataRef)connection.responseData;
  CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData(imageData);
  CGImageRef image = CGImageCreateWithPNGDataProvider(imageDataProvider, NULL, true, kCGRenderingIntentDefault);
  if (!image) image = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, true, kCGRenderingIntentDefault);
  UIImage* anImage = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
  CGDataProviderRelease(imageDataProvider);
  CGImageRelease(image);

The other point is that you also need to do the same for the EGOCache class. I'm using that :

- (UIImage*)imageForKey:(NSString*)key {
  NSData* d = [[NSFileManager defaultManager] contentsAtPath:cachePathForKey(key)];
  if (d) {
    CFDataRef imageData = (CFDataRef) d;
    CGDataProviderRef imageDataProvider = CGDataProviderCreateWithCFData(imageData);
    CGImageRef image = CGImageCreateWithPNGDataProvider(imageDataProvider, NULL, true, kCGRenderingIntentDefault);
    if (!image) image = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, true, kCGRenderingIntentDefault);
    UIImage* anImage = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
    CGDataProviderRelease(imageDataProvider);
    CGImageRelease(image);
    return anImage;
  } else {
    return nil;
  }  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment