2

UIImageView 内に表示される UIImage で奇妙な問題が発生しています。

173x173 の画像ビュー内に 1024x1024 の画像を配置し、「塗りつぶしに合わせて拡大縮小」を設定すると、結果がエイリアス化されます。ただし、同じ画像を入れて、imageview のスクリーンショットを撮り、それを元に戻すと、画像は完全に表示されます。スクリーンショットは、画像ビュー内で見ている正確なピクセルであるはずなので、これは気になります。

左側は画像を直接割り当てた結果です。右側は、同じ画像のスクリーンショットを撮り、それを割り当てています。Es が左側ではギザギザになっていますが、右側では滑らかになっています。

ここに画像の説明を入力

これは、画像ビューのコンテンツ モードの問題ですか、それともスクリーン ショット中に何かが起こりますか?

    for(UIImageView* placeholderView in self.placeholderImageViews)
    {
//assigning a 1024x1024 image into a smaller image view results in an aliased image here
        placeholderView.image = imageToDisplay;

//this code makes the image appear perfectly scaled down
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        {
            UIGraphicsBeginImageContextWithOptions(placeholderView.frame.size, NO, [UIScreen mainScreen].scale);
        }
        else
        {
            UIGraphicsBeginImageContext(placeholderView.frame.size);
        }
        [placeholderView.layer renderInContext:UIGraphicsGetCurrentContext()];
        screenshot = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        //save the result
        placeholderView.image = screenshot;

    }
4

1 に答える 1

0

UIImageView のスケーリングは最悪です。常に境界とまったく同じサイズの画像を指定する必要があるため、縮小する必要はありません。このコードと例を確認してください。

スウィフト拡張:

extension UIImage{

        // returns a scaled version of the image
        func imageScaledToSize(size : CGSize, isOpaque : Bool) -> UIImage{

            // begin a context of the desired size
            UIGraphicsBeginImageContextWithOptions(size, isOpaque, 0.0)

            // draw image in the rect with zero origin and size of the context
            let imageRect = CGRect(origin: CGPointZero, size: size)
            self.drawInRect(imageRect)

            // get the scaled image, close the context and return the image
            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            return scaledImage
       }
}

例:

aUIImageView.image = aUIImage.imageScaledToSize(aUIImageView.bounds.size, isOpaque : false)

画像にアルファがない場合は isOpaque を true に設定します。描画のパフォーマンスが向上します。

于 2015-06-17T14:31:05.800 に答える