UIImage+RoundedCorner 与 SDWebImage
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于调整图像大小部分,我没有一个好的答案。
对于圆角功能,你完全走错了路,这就是我之前尝试做的...尝试调整每个图像的大小和圆角并将其保存到磁盘......太复杂,太多要做的事情...
正确且简单的方法是设置表格单元格的 UIImageViewcornerRadius:
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:
我明白了这一点。
您应该在 SDImageCache.m 中使用以下方法操作图像:
在第三种方法中,您必须记住使用以下代码将 UIImage 转换为 NSData。如果
toDisk
为 true,则应执行此操作。使用
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:
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.Using
if (croppedRoundedImageData) data = croppedRoundedImageData;
your app will not crash if your data isNULL
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)SDWebImageManagerDelegate 协议就是您正在寻找的。
https://github.com/rs/SDWebImage/blob/master /SDWebImage/SDWebImageManager.h#LC96
我在 AppDelegate 类中实现了我们需要的方法。
您可以告诉sharedManager 像这样使用您的委托。
我在 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.
You can tell the sharedManager to use your delegate like this.
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.
这是一个很棒的发现,但我发现通过在 SDWebImageDownloader.m 中添加图像操作代码,它可以使用 SDWebImage 为所有图像制作缩略图。我所做的是创建一个 SDWebImageManager 实例并使用以下方法:
创建图像,唯一的问题是因为图像出现的时间不同,我试图找到一种方法将它们与操作正确链接。
尽管如此,答案还是很好的。
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:
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.
我使用 SDWebImage 框架,它不给我修改文件的能力,但我有一个像这样的全局函数:
和像这样缩放图像的全局函数:
Im using SDWebImage framework which doesn't give me the ability to modify the files but i have a global function like this:
and global funtion to scale image like this: