1

我有一个大的显示区域,可以平移和缩放以查看不同的对象。我遇到的问题是,如果我缩小,PNG 图像 UIButton 的质量会有所下降(但是当我放大到 100% 时它会恢复正常)。它几乎看起来好像图像变得过度锐化。这是我必须忍受的东西,还是有办法摆脱这种颗粒状的边缘效应?顺便说一下,图像的纵横比始终是 1:1。

4

1 回答 1

0

我能够通过使用在我的 scrollViewDidEndZooming 方法中找到的答案来解决这个问题。这是我的代码:

调整大小功能

- (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;
    }
}
于 2012-05-13T16:44:03.803 回答