我使用SDWebImage下载缩略图。我在使用- (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL
委托方法缓存它们之前调整它们的大小。
我用来重新分配图片的代码是:
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize Image:(UIImage *)image
{
UIImage *sourceImage = image;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor) {
scaleFactor = widthFactor; // scale to fit height
} else {
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else {
if (widthFactor < heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
}
UIGraphicsBeginImageContextWithOptions(targetSize, YES, 0); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
首先,我使用了UIGraphicsBeginImageContext(targetSize);
,但视网膜上模糊的图像因此我将功能更改为UIGraphicsBeginImageContextWithOptions(targetSize, YES, 0);
但现在我的图像在我的TableView单元格中显得更大了。似乎调整大小代码会生成更高清晰度的图像,但SDWebImage库并不知道如何正确加载它们。
正确尺寸的图像位于底部,顶部图像的尺寸不正确。
提前感谢您的帮助
答案 0 :(得分:1)
想出来,
您可以通过将图像设置为UIImage imageWithCGImage:image.CGImage scale:[[UIScreen mainScreen] scale] orientation:image.imageOrientation];
来指定加载视网膜图像的UIImageView