2015-06-04 51 views
1

我有一个大的徽标图像(1000x500)。我想使用这个图像,并将其绘制在一个尺寸为100x100的框架中。下面是我如何做到这一点:缩小的图像是以非常低的质量绘制的?

self.logo = UIImageView(image: UIImage(named: "logoBig")!) 
self.logo?.contentMode = UIViewContentMode.ScaleAspectFit 
self.logo?.frame = CGRectMake(self.view.bounds.width/2 - 50, 50, 100, 100) 
self.view.addSubview(self.logo!) 

This works。问题是在缩小图像尺寸后图像质量非常低。

如何在不失去质量的情况下将我的大图像绘制到较小的框架中?

回答

0

尝试这样的:

let logo = UIImageView(frame: CGRectMake(view.bounds.midX - 50, 50, 100, 100)) 
logo.contentMode = .ScaleAspectFit 
logo.image = UIImage(named: "logoBig")! 
view.addSubview(logo) 
+0

如果我想再次增加图像,该怎么办。我如何确保我不会失去质量? – confile

+0

更改uiimageview框架并设置另一个图像或相同图像 –

0

尝试使用这种方法

- (UIImage*)resizeImage:(UIImage*)image scaledToSize:(CGSize)newSize{ 
    //UIGraphicsBeginImageContext(newSize); 
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution). 
    // Pass 1.0 to force exact pixel size. 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return newImage; 
} 

,如果你想重新调整图像大小,保留原始图像,并将其用于不同的尺寸。

添加备注: 此外,原始图像大小为1000x500,调整大小会使图像的宽高比变得松散,因为100x100不能与原始图像大小进行比较。

+0

我使用ScaleAspectFit,因此没有宽高比的问题。 – confile

+0

然后,你很好去...:thumbsup: – MetaSnarf

+0

如果我的回答帮助你,请接受。 – MetaSnarf

相关问题