从URL到iPhone和iPhone视网膜获取UIImage

时间:2012-06-18 10:10:28

标签: iphone uiimage retina-display

我正在尝试从URL获取图像,以便在iPhone和iPhone Retina上查看它。 问题是iPhone显示正确,但Retina模糊。 该图像的大小为100x100,326dpi(视网膜大小)。

我做得对吗?

    - (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

谢谢!

iPhone正常 iPhone Retina

2 个答案:

答案 0 :(得分:0)

你通知服务器给我一个视网膜双倍大小的图像。如果正常iphone的100x100图像应该是视网膜尺寸的两倍;如果相同的图像图像会模糊。

尝试此代码以确定视网膜屏幕

  + (BOOL)isRetineDisplay{
        if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] &&
            ([UIScreen mainScreen].scale == 2.0)) {
            // Retina display
            return YES;
        } else {
            // not Retine display
            return NO;
        }
    }

答案 1 :(得分:0)

修改

- (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;
}