从服务器调整大小和放置 Retina UIImage



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

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

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

您是否试图为视网膜显示器强行将任何图像放大两次?如果是,图像的质量不会变得更好。这是合理的。如您所知,图像的质量不能通过简单地拉伸宽度和高度来提高。

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

相关内容

  • 没有找到相关文章

最新更新