0
我知道這已經被問過......但我認爲我做的一切都很好,但是圖像在視網膜顯示上看起來不太好。我聽說如果需要在視網膜上顯示UIImage,我所要做的就是將縮放比例設置爲2.0。但仍然無法正常工作,我不知道UIGraphics調整大小和裁剪是否與圖像質量混淆。通常我會在自定義的UIImageView類中調用我的Resizer(從服務器的圖像,它具有正確的寬度,但是更高,所以它只能裁剪),在我的控制器中被稱爲像這樣:從服務器調整大小並放置視網膜UIImage
[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;
}
我已經調試...這大小和規模是正確的。我不知道是什麼影響了圖像的質量。一個簡單的解釋將有助於很多,thx。
確定,但原始圖像的寬度爲* = 2;甚至比高* 2更高; ...實際上非視網膜必須使其更小。但無論如何,我發現我的錯誤。它不是UIImageView的子類... insead它是一個custum類的子類,它是UIImageView的子類,它也具有setImage:override,所以它在該override中製作其他類型的調整大小,這使得奇怪的結果...但我給你+1試圖幫助:) – user2387149