【问题标题】:SDWebImage resizing with UIGraphicsBeginImageContextWithOptionsSDWebImage 使用 UIGraphicsBeginImageContextWithOptions 调整大小
【发布时间】:2015-07-09 00:15:03
【问题描述】:

我正在使用 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 库不知道如何正确加载它们。

正确尺寸的图片位于底部,顶部的尺寸不正确。

提前感谢您的帮助

【问题讨论】:

    标签: ios objective-c uiimage sdwebimage


    【解决方案1】:

    想通了

    您可以通过将图像设置为 UIImage imageWithCGImage:image.CGImage scale:[[UIScreen mainScreen] scale] orientation:image.imageOrientation]; 来指定它正在加载视网膜图像的 UIImageView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      相关资源
      最近更新 更多