0

我正在尝试从 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];
}

普通屏幕的图像 图像模糊

谢谢!

iPhone 普通 iPhone 视网膜

4

2 回答 2

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;
}
于 2012-06-18T10:34:44.257 回答
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;
        }
    }
于 2012-06-18T10:31:51.543 回答