0

これは以前に尋ねられたことを知っています...しかし、私はすべてうまくやっていると思いますが、Retinaディスプレイでは画像がうまく見えません。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;
}

私はデバッグしています...そして、このサイズとスケールは正しいものです。何が画質を乱しているのかわかりません。簡単な説明は大いに役立ちます、thx。

4

1 に答える 1

1

Retina ディスプレイ用に任意の画像を強制的に 2 倍に拡大しようとしましたか? はいの場合、画質は向上しません。合理的です。ご存じのとおり、幅と高さを単純に引き延ばしても画質は向上しません。

本当に必要なのは、元々2倍の解像度を持つ高解像度の画像を使用することです。

于 2014-02-13T01:47:43.650 に答える