UIImage+RoundedCorner 与 SDWebImage

发布于 2024-11-29 17:01:42 字数 747 浏览 2 评论 0原文

我正在使用 SDWebImage 将图像加载到我的表格视图中。我希望调整表格视图中的图像大小并具有圆角。我发现 UIImage+Resize 和 UIImage+ RoundedCorner 来做到这一点。这两个库单独工作都很好,但我无法将它们结合起来。我可以调整 SDWebImage 返回的图像的大小和圆角,但我发现这相当耗费资源,因此我想在将图像保存到缓存之前调整图像的大小。 当第一次从网络加载图像时,它可能会在保存到缓存之前显示,因此我还想在第一次加载时调整图像的大小。

我无法执行此操作,因为我无法弄清楚使用 SDWebImage 的哪种方法来操作图像。我所需要做的就是在 SDWebImage 中的右侧 UIImage 上调用以下内容。

UIImage *image = [image thumbnailImage:50 transparentBorder:0 cornerRadius:5 interpolationQuality:kCGInterpolationHigh];

谁能告诉我应该在 SDWebImage 中的什么位置放置这段代码,以便在将图像保存到缓存之前对图像进行操作,并在从互联网而不是缓存加载时将操作后的图像发送到图像视图?

I am using SDWebImage to load images into my table view. I would like the images in my table view to be resized and have round corners. I found UIImage+Resize and UIImage+RoundedCorner to do this. Both libraries works great seperately but I have not been abl to combine them. I could resize and round the corners of the image SDWebImage returns but I have found this to be fairly resource heavy and therefore I would like to have the images resized before saving them to the cache.
When an image is loaded from the net for the first time it is probably shown before being saved to the cache therefore I would also like to resize the image when it is loaded for the first time.

I have not been able to do this as I cannot figure out which method of SDWebImage to manipulate the image in. All I need is to call the following on the right UIImage in SDWebImage.

UIImage *image = [image thumbnailImage:50 transparentBorder:0 cornerRadius:5 interpolationQuality:kCGInterpolationHigh];

Can anyone tell me where in SDWebImage I should place this piece of code to have the image manipulated before being saved to the cache and have a manipulated image sent to the image view when it is loaded from the internet and not the cache?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

入画浅相思 2024-12-06 17:01:42

对于调整图像大小部分,我没有一个好的答案。

对于圆角功能,你完全走错了路,这就是我之前尝试做的...尝试调整每个图像的大小和圆角并将其保存到磁盘......太复杂,太多要做的事情...

正确且简单的方法是设置表格单元格的 UIImageViewcornerRadius:

cell.imageView.layer.cornerRadius = 8;
cell.imageView.layer.masksToBounds = YES;

For the resize image part, I don't have a good answer.

For the round corners feature, you are completely going to the wrong way, this is what I try to do before... try to resize and round every image and save it to the disk...... too complex, too many things to do...

the correct and simple way is to set the UIImageView cornerRadius of your table cell:

cell.imageView.layer.cornerRadius = 8;
cell.imageView.layer.masksToBounds = YES;
毁我热情 2024-12-06 17:01:42

我明白了这一点。

您应该在 SDImageCache.m 中使用以下方法操作图像:

 1. - (void)storeImage:(UIImage *)image forKey:(NSString *)key;
 2. - (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk;
 3. - (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)key toDisk:(BOOL)toDisk;

在第三种方法中,您必须记住使用以下代码将 UIImage 转换为 NSData。如果 toDisk 为 true,则应执行此操作。

NSData *croppedRoundedImageData = UIImageJPEGRepresentation(croppedRoundedImage, 1.0);
if (croppedRoundedImageData) data = croppedRoundedImageData;

使用 if (croppedRoundedImageData) data =croppedRoundedImageData; 如果您尝试保存数据时 NULL,您的应用程序不会崩溃。

在 SDWebImageDownloader.m 中,您必须添加用于操作的代码 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection 这用于图像的第一次加载(不在缓存中时)

I figured this out.

You should manipulate the image in the following methods in SDImageCache.m:

 1. - (void)storeImage:(UIImage *)image forKey:(NSString *)key;
 2. - (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk;
 3. - (void)storeImage:(UIImage *)image imageData:(NSData *)data forKey:(NSString *)key toDisk:(BOOL)toDisk;

In the third method you must remember to convert your UIImage to NSData using the code below. This should be done if toDisk is true.

NSData *croppedRoundedImageData = UIImageJPEGRepresentation(croppedRoundedImage, 1.0);
if (croppedRoundedImageData) data = croppedRoundedImageData;

Using if (croppedRoundedImageData) data = croppedRoundedImageData; your app will not crash if your data is NULL when you try to save it.

In SDWebImageDownloader.m you must add your code for manipulating in - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection This is used for the first load of the image (when not in cache)

深者入戏 2024-12-06 17:01:42

SDWebImageManagerDelegate 协议就是您正在寻找的。

https://github.com/rs/SDWebImage/blob/master /SDWebImage/SDWebImageManager.h#LC96

我在 AppDelegate 类中实现了我们需要的方法。

#pragma mark - SDWebImageManagerDelegate

-(UIImage*) imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL{

    UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);

    CGRect box = CGRectMake(0, 0, image.size.width, image.size.height);
    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:box
                                cornerRadius:15.f] addClip];
    // Draw your image
    [image drawInRect:box];

    // Get the image, here setting the UIImageView image
    UIImage* ret = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();
    return ret;

}

您可以告诉sharedManager 像这样使用您的委托。

SDWebImageManager* imageManager = [SDWebImageManager sharedManager];
imageManager.delegate = self;

我在 didFinishLaunchingWithOptions 中做到了这一点。

请记住,这种方法会按照图像的原始大小沿着图像的边界进行剪辑。如果图像的边界渲染在视图边界之外(长宽比填充等),您将看不到圆角。

这种方法比每次渲染图像时剪切图像的性能更高,但请注意上述缺点。

The SDWebImageManagerDelegate protocol is what you're looking for.

https://github.com/rs/SDWebImage/blob/master/SDWebImage/SDWebImageManager.h#LC96

I implemented the method we need in my AppDelegate class.

#pragma mark - SDWebImageManagerDelegate

-(UIImage*) imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL{

    UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);

    CGRect box = CGRectMake(0, 0, image.size.width, image.size.height);
    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:box
                                cornerRadius:15.f] addClip];
    // Draw your image
    [image drawInRect:box];

    // Get the image, here setting the UIImageView image
    UIImage* ret = UIGraphicsGetImageFromCurrentImageContext();

    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();
    return ret;

}

You can tell the sharedManager to use your delegate like this.

SDWebImageManager* imageManager = [SDWebImageManager sharedManager];
imageManager.delegate = self;

I did this in didFinishLaunchingWithOptions.

Keep in mind that this approach clips along the bounds of the image at it's native size. If the bounds of your image are being rendered outside the bounds of your view (aspect-fill, etc), you will not see the rounded corners.

This approach is more performant than clipping the image every time it's rendered, but be aware of the above disadvantage.

后eg是否自 2024-12-06 17:01:42

这是一个很棒的发现,但我发现通过在 SDWebImageDownloader.m 中添加图像操作代码,它可以使用 SDWebImage 为所有图像制作缩略图。我所做的是创建一个 SDWebImageManager 实例并使用以下方法:

- (void) webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image; 

创建图像,唯一的问题是因为图像出现的时间不同,我试图找到一种方法将它们与操作正确链接。

尽管如此,答案还是很好的。

This is a great find, but I find that by adding your image manipulation code in SDWebImageDownloader.m it makes thumbnails for all images using SDWebImage. What I did was create an SDWebImageManager instance and used the method:

- (void) webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image; 

to create the images, the only problem is because the images come in different times I'm trying to find a way to link them properly with an action.

Great answer nonetheless.

智商已欠费 2024-12-06 17:01:42

我使用 SDWebImage 框架,它不给我修改文件的能力,但我有一个像这样的全局函数:

     + (UIImage*)circleImageWithImage:(UIImage*)image
      {
          UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
          CALayer *layer = [imageView layer];
          [layer setMasksToBounds:YES];
          [layer setCornerRadius:imageView.frame.size.width / 2];

          UIGraphicsBeginImageContext(image.size);
          [layer renderInContext:UIGraphicsGetCurrentContext()];
          UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return newImage;
      }

和像这样缩放图像的全局函数:

      + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
      {   
          UIGraphicsBeginImageContext(newSize);
          [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
          UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return newImage;
      }

Im using SDWebImage framework which doesn't give me the ability to modify the files but i have a global function like this:

     + (UIImage*)circleImageWithImage:(UIImage*)image
      {
          UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
          CALayer *layer = [imageView layer];
          [layer setMasksToBounds:YES];
          [layer setCornerRadius:imageView.frame.size.width / 2];

          UIGraphicsBeginImageContext(image.size);
          [layer renderInContext:UIGraphicsGetCurrentContext()];
          UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return newImage;
      }

and global funtion to scale image like this:

      + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
      {   
          UIGraphicsBeginImageContext(newSize);
          [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
          UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
          return newImage;
      }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文