我有一个大的显示区域,可以平移和缩放以查看不同的对象。我遇到的问题是,如果我缩小,PNG图像UIButton的质量会有所下降(但是当我缩小到100%时它会恢复正常)。它看起来好像图像变得过度锐化。这是我将不得不忍受的东西,还是有办法摆脱这种颗粒状的边缘效应?顺便说一句,图像的纵横比总是1:1。
答案 0 :(得分:0)
我能够通过在scrollViewDidEndZooming方法中找到here找到的答案来解决这个问题。这是我的代码:
调整功能
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
return newImage;
}
ScrollView方法 (Widget是一个UIViewController子类,它包含一个按钮和一个“widgetImage”,它存储按钮应显示的图像的完整分辨率)
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
for(Widget *theWidget in widgets){
UIImage *newScaledImage = [self resizeImage:theWidget.widgetImage newSize:CGSizeMake(theWidget.view.frame.size.width * scale, theWidget.view.frame.size.height * scale)];
[theWidget.widgetButton setImage:newScaledImage forState:UIControlStateNormal];
// theWidget.widgetButton.currentImage = newScaledImage;
}
}