【问题标题】:Downloading images and retina display下载图像和视网膜显示
【发布时间】:2014-03-16 02:42:48
【问题描述】:

如果我从服务器下载一些图像,我应该调整它的大小以支持视网膜设备吗?

如果现在我不能使用 -@2x,我该如何设置正确的图像来显示?

更新:

如果我缓存图片,我需要在下载后缓存前调整图片大小,并缓存2张图片,常规和@2x?

【问题讨论】:

    标签: ios image uiviewcontroller retina


    【解决方案1】:

    您的应用应该以视网膜大小检索这些图像。要获得非视网膜尺寸,您可以手动缩放然后保存。这里是示例代码:

    UIImage *image = [UIImage imageNamed:@"yourRetinaImage@2x.png"];
    
    // set non-retina size from current image
    CGSize size = CGSizeMake(image.size.width / 2., image.size.height / 2.);
    
    
    /** scale the image */
    
    UIGraphicsBeginImageContext(size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), image.CGImage);
    
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    /** save scaled image */
    
    NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    // save with same name but without suffix "@2x"
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", basePath, @"yourRetinaImage"];
    
    @try {
        [UIImagePNGRepresentation(scaledImage) writeToFile:filePath options:NSAtomicWrite error:nil];
    } @catch (NSException *exception) {
        NSLog(@"error while saving non-retina image with exception %@", exception);
    }
    

    【讨论】:

    • thanks 看起来不错,如果我缓存图片,我需要在下载后缓存之前使用上面的代码,并缓存2张图片,常规和@2x?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多