简体   繁体   English

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

[英]Get an UIImage from URL to iPhone and iPhone retina

I'm trying to get an image from a URL to view it on an iPhone and an iPhone Retina. 我正在尝试从URL获取图像以在iPhone和iPhone Retina上查看。 The problem is that iPhone is displayed correctly but Retina is blurred. 问题是iPhone正确显示,但视网膜模糊。 The image has a size of 100x100 at 326dpi (size retina). 图像在326dpi时的尺寸为100x100(视网膜尺寸)。

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

正常屏幕的图像图像模糊

Thank you! 谢谢!

iPhone Normal iPhone Retina iPhone普通iPhone视网膜

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; 如果普通iPhone的图片为100x100,则视网膜的图片尺寸应为原来的两倍; 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;
        }
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM