我知道,縮小圖像時,您必須期望質量有所下降,但是當我將圖像分配到尺寸爲(75,75)的UIButton時,質量很好。縮放時圖像質量下降
當我使用UIPasteboard將圖像縮放到尺寸(75,75)進行復制/粘貼時,它的質量非常差。
背景:我的應用程序是一個鍵盤的擴展,所以我有按鈕具有指定的圖像,並單擊時,我從按鈕獲取圖像,它的規模是合適的大小,將它複製到UIPasteboard,然後糊。
代碼:
這裏是我的檢測按鈕,點擊複製圖像代碼:
- (IBAction) clickedImage:(id)sender {
UIButton *btn = sender;
UIImage *scaledImage = btn.imageView.image;
UIImage *newImage = [scaledImage imageWithImage:scaledImage andSize:CGSizeMake(75, 75)];
NSData *imgData = UIImagePNGRepresentation(newImage);
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];
}
而且我有用於縮放圖像的imageWithImage:andSize:
方法的UIImage的類別。這是縮放方法:
- (UIImage*)imageWithImage:(UIImage*)image andSize:(CGSize)newSize {
// Create a bitmap context.
UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
什麼沒有意義的是,當我把在UIButton的被縮小到完全相同的尺寸,當我使用擴展碼圖像,但質量的方法更好的UIButton比當我返回縮放圖像。我的縮放代碼有問題嗎?有誰知道爲什麼兩幅圖像之間的質量會有這樣的下降?
您正在從按鈕中檢索圖像。如果您從資源創建新的「UIImage」,它是否一樣? – Losiowaty
@Losiowaty是的,我試過創建一個新的UIImage,它是一樣的 –
這些圖像可以更好地表示爲矢量圖形,然後以任何縮放比例輸出嗎? –