0

I'm trying to get an image from a URL to view it on an iPhone and an iPhone Retina. The problem is that iPhone is displayed correctly but Retina is blurred. The image has a size of 100x100 at 326dpi (size retina).

I'm doing it correctly?

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    double scaleFactor = [UIScreen mainScreen].scale;
    NSURL *imageURL = [NSURL URLWithString:@"http://s419999211.mialojamiento.es/img/bola.png"];

    if (scaleFactor == 2){
        // @2x
        NSLog(@"Estoy cargando la imágen retina");
        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        image = [UIImage imageWithData:imageData];
        NSLog(@"Width: %f Height: %f",image.size.width,image.size.height);
        yourImageView = [[UIImageView alloc] initWithImage:image];
    } else {
        // @1x
        NSLog(@"Estoy cargando la imágen normal");
        NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
        image = [UIImage imageWithData:imageData];
        imagenScalada = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationUp];
        NSLog(@"Width: %f Height: %f",imagenScalada.size.width,imagenScalada.size.height);
        yourImageView = [[UIImageView alloc] initWithImage:imagenScalada];
    }

    [self.view addSubview:yourImageView];
}

Image for normal screen Imagen blurred

Thank you!

iPhone Normal iPhone Retina

dpbataller
  • 1,197
  • 4
  • 12
  • 23

2 Answers2

0

where u notify to server that give me a double size image for retina. If 100x100 image for normal iphone than it should be double in size for retina; If u same image image will blurred.

try this code for determine retina screen

  + (BOOL)isRetineDisplay{
        if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
            ([UIScreen mainScreen].scale == 2.0)) {
            // Retina display
            return YES;
        } else {
            // not Retine display
            return NO;
        }
    }
gauravds
  • 2,931
  • 2
  • 28
  • 45
  • The image is 100x100 for Retina. Then for iPhone normal, should be 50x50 not? So I try to scale it to normal size (50x50), but does not work ... My actuality code, shows when you run in normal screen and when you run in retina screen... – dpbataller Jun 18 '12 at 10:46
0

EDIT:

- (void)viewDidLoad
{
    [super viewDidLoad];

    double scaleFactor = [UIScreen mainScreen].scale;
    NSURL *imageURL = [NSURL URLWithString:@"http://s419999211.mialojamiento.es/img/bola@2x.png"];
    NSData * imageData = nil;
    if (scaleFactor == 2){
        imageData = [NSData dataWithContentsOfURL:imageURL2x];
        image = [UIImage imageWithData:imageData];
    }
    else {
        imageData = [NSData dataWithContentsOfURL:imageURL2x];
        image = [UIImage imageWithData:imageData];
        image = [self scaledImage:image];
    }



    yourImageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:yourImageView];
}


- (UIImage *)scaledImage:(UIImage *)image {
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); //Change the size of the image
    UIGraphicsBeginImageContext(rect.size);

    [image drawInRect:rect];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    return scaledImage;
}
DLende
  • 5,162
  • 1
  • 17
  • 25
  • Thanks for answering! At first, I did what you said and it works. I want to avoid having two images on the server. This was only a test, but in the future will have many images and can be very uncomfortable. So my intention is to produce a single image and scale it. – dpbataller Jun 18 '12 at 10:43
  • You can store a retina image on the server. Download the image and convert it to small size – DLende Jun 18 '12 at 10:49
  • Exactly! that's what I'm trying ... It does not work. The steps I perform are the following: 1) I check if normal screen or retina with an if / else 2) Release the retinal image in the server, if is retina display, show the image downloaded. But if normal screen, change its size with imagenScalada = [UIImage imageWithCGImage: [image CGImage] scale: 1.0 orientation: UIImageOrientationUp]; Then run the application with normal screen and the image became small (seems to work) but if I run the simulator in the retina, the image is blurred – dpbataller Jun 18 '12 at 10:58
  • This are the sizes of the images http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html – DLende Jun 18 '12 at 11:03