Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从URL获取iPhone和iPhone视网膜的UIImage_Iphone_Uiimage_Retina Display - Fatal编程技术网

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

从URL获取iPhone和iPhone视网膜的UIImage,iphone,uiimage,retina-display,Iphone,Uiimage,Retina Display,我正在尝试从URL获取一张图像,以便在iPhone和iPhone视网膜上查看它。 问题是iPhone显示正确,但视网膜模糊。 图像的大小为100x100,分辨率为326dpi(视网膜大小) 我做得对吗 - (void)viewDidLoad { [super viewDidLoad]; double scaleFactor = [UIScreen mainScreen].scale; NSURL *imageURL = [NSURL URLWithStr

我正在尝试从URL获取一张图像,以便在iPhone和iPhone视网膜上查看它。 问题是iPhone显示正确,但视网膜模糊。 图像的大小为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];
}

谢谢大家!

iphonenormal
iPhone Retina

在这里,你通知服务器给我一张两倍大小的Retina图像。如果普通iphone的图像为100x100,那么视网膜的大小应该是普通iphone的两倍;如果你是同一个图像,图像就会模糊

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

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

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

谢谢你的回答!一开始,我照你说的做了,效果很好。我希望避免在服务器上有两个映像。这只是一个测试,但在未来会有很多图像,可能会非常不舒服。所以我的目的是生成一个图像并缩放它。你可以在服务器上存储一个视网膜图像。下载图像并将其精确转换为小尺寸!这就是我要尝试的。。。它不起作用。我执行的步骤如下:1)我用if/else检查屏幕是否正常或视网膜是否正常2)在服务器中释放视网膜图像,如果是视网膜显示,则显示下载的图像。但如果屏幕正常,则使用imagenScalada=[UIImage imageWithCGImage:[image CGImage]缩放:1.0方向:UIImageOrientationUp]更改其大小;然后在正常屏幕上运行应用程序,图像变小(似乎正常),但如果我在视网膜上运行模拟器,图像会变得模糊这是图像的大小视网膜的图像是100x100。那么对于iPhone正常,应该是50x50不是吗?所以我尝试将其缩放到正常大小(50x50),但不起作用。。。我的现状代码,显示当你们在正常屏幕上运行,当你们在视网膜屏幕上运行。。。