我知道以前有人问过这个问题......但我认为我做的一切都很好,但图像在视网膜显示器上看起来不太好。我听说如果需要在视网膜中显示 UIImage 我所要做的就是将比例设置为 2.0。但仍然无法正常工作我不知道 UIGraphics 调整大小和裁剪是否会影响图像质量。通常我会从 UIImageView 的自定义类中的 setImage 调用我的调整器(来自服务器的图像它具有正确的宽度,但更高,所以它假设只裁剪)在我的控制器中我会这样称呼它:
[customUIImageViewInstance setImage:[UIImage imageWithData:[impreso thumb] scale:isRetina()?2:1]];
在我的自定义 UIImageView 实现中:
-(void)setImage:(UIImage *)image{
if (image==nil) {
[super setImage:nil];
}else{
[super setImage:[self scaleAnImageAspectFillCropHeight:image withNewWide:self.frame.size.width andNewTall:self.frame.size.height]];
}
}
-(UIImage*) scaleAnImageAspectFillCropHeight:(UIImage*) image withNewWide:(NSUInteger) wide andNewTall:(NSUInteger) tall{
if (image.size.width!=wide || image.size.height!=tall){
BOOL retina;
if ([image scale]==2) {
retina = YES;
wide*=2;
tall*=2;
}else{
retina = NO;
}
CGFloat width = image.size.width;
CGFloat height = image.size.height;
CGFloat targetWidth = wide;
CGFloat targetHeight = tall;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth;
CGFloat scaledHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
scaleFactor = widthFactor; // the width must be all visible, the height might be cropped but that is the inteded behavior
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// centers X if needed, Y starts in 0 because the top part of the image is important
thumbnailPoint.y = 0;
if (widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
UIGraphicsBeginImageContext(CGSizeMake(wide, tall)); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[image drawInRect:thumbnailRect];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
if (retina) {
newImage = [UIImage imageWithCGImage:newImage.CGImage scale:2 orientation:newImage.imageOrientation];
}
if(newImage == nil){
[self setContentMode:UIViewContentModeScaleAspectFit];
}else{
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
}
return image;
}
我一直在调试……这个尺寸和比例是正确的。我不知道是什么弄乱了图像的质量。一个简单的解释会很有帮助,谢谢。