ios - 调整大小并放置来自服务器的 Retina UIImage

标签 ios uiimage retina-display uigraphicscontext

我知道以前有人问过这个问题...但我认为我做的一切都很好,但图像在视网膜显示屏上看起来不太好。我听说如果需要在视网膜上显示 UIImage 我所要做的就是将比例设置为 2.0。但仍然无法工作,我不知道 UIGraphics 调整大小和裁剪是否会影响图像质量。通常我会从自定义类 UIImageView 中的 setImage 调用我的调整器(来自服务器的图像具有正确的宽度,但更高,所以它应该只裁剪)在我的 Controller 中我会这样调用它:

[customUIImageViewInstance setImage:[UIImage imageWithData:[impreso thumb] scale:isRetina()?2:1]];

在我的自定义 UIImageView 实现中:

-(void)setImage:(UIImage *)image{
    if (image==nil) {
        [super setImage:nil];
    }else{
        [super setImage:[self scaleAnImageAspectFillCropHeight:image withNewWide:self.frame.size.width andNewTall:self.frame.size.height]];
    }
}



-(UIImage*) scaleAnImageAspectFillCropHeight:(UIImage*) image withNewWide:(NSUInteger) wide andNewTall:(NSUInteger) tall{
    if (image.size.width!=wide || image.size.height!=tall){
        BOOL retina;
        if ([image scale]==2) {
            retina = YES;
            wide*=2;
            tall*=2;
        }else{
            retina = NO;
        }
        CGFloat width = image.size.width;
        CGFloat height = image.size.height;
        CGFloat targetWidth = wide;
        CGFloat targetHeight = tall;
        CGFloat scaleFactor = 0.0;
        CGFloat scaledWidth;
        CGFloat scaledHeight;
        CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    CGFloat widthFactor = targetWidth / width;
    CGFloat heightFactor = targetHeight / height;

    scaleFactor = widthFactor; // the width must be all visible, the height might be cropped but that is the inteded behavior

    scaledWidth  = width * scaleFactor;
    scaledHeight = height * scaleFactor;

    // centers X if needed, Y starts in 0 because the top part of the image is important
    thumbnailPoint.y = 0;
    if (widthFactor < heightFactor){
        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    }


    UIGraphicsBeginImageContext(CGSizeMake(wide, tall)); // this will crop

    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;

    [image drawInRect:thumbnailRect];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    if (retina) {
        newImage = [UIImage imageWithCGImage:newImage.CGImage scale:2 orientation:newImage.imageOrientation];
    }

    if(newImage == nil){
        [self setContentMode:UIViewContentModeScaleAspectFit];
    }else{
        //pop the context to get back to the default
        UIGraphicsEndImageContext();
        return newImage;
    }
}
return image;
}

我一直在调试……这个尺寸和比例是正确的。我不知道是什么影响了图像质量。简单的解释会有很大帮助,谢谢。

最佳答案

您是否试图为视网膜显示器强行将任何图像放大两次?如果是,则图像质量不会变好。这是合理的。您可能知道,简单地拉伸(stretch)宽度和高度不会改善图像质量。

您真正需要做的是使用原本具有两倍分辨率的高分辨率图像。

关于ios - 调整大小并放置来自服务器的 Retina UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21743491/

相关文章:

ios - 键盘出现时如何调整ScrollView的大小?

iOS Swift - 自定义 UITableViewCell

ios - 具有独立视频和音频 url 的 AVAsset (iOS)

ios - 不同形状的图像拼贴

iphone - 如何关闭 UIPopoverController?

ios - 使用异步下载后将 UIImage 保存到核心数据或图像路径

UIImageView 到 UIImage

css - 如何将 srcset 和 sizes 用于响应式图像

javascript - 在显示器之间拖动浏览器窗口时,devicePixelRatio 会发生变化吗?

iphone - iPhone 应用程序的 iPad 内容比例因子 x2