UIImageView,setClipsToBounds以及我的图像如何失去理智 [英] UIImageView, setClipsToBounds and how my images are losing their head

查看:256
本文介绍了UIImageView,setClipsToBounds以及我的图像如何失去理智的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS 4应用程序。

I'm developing an iOS 4 application.

我在 UIImageView 上使用此代码 UITableViewCell

cell.photo.contentMode = UIViewContentModeScaleAspectFill;
[cell.photo setClipsToBounds:YES];

我的图像高于宽,当我设置 UIViewContentModeScaleAspectFill ,缩放图像以适应UIImageView宽度的宽度。这样做,UIImageView的图像越大,我在 UIImageView 外面看到它。

My images are taller than wide, and when I set UIViewContentModeScaleAspectFill, images are scaled to fit width with UIImageView width. Doing this, the image gets bigger that UIImageView and I see it outside UIImageView.

然后,我需要使用 setClipsToBounds:YES 不允许这样做,但现在我的所有图片都失去了理智。

Then, I need to use setClipsToBounds:YES to don't allow this, but now all my images lose their head.

有没有如何使用UIViewContentModeScaleAspectFill和左上角的左上角图像角 UIImageView 角?如果我需要丢失图像的一部分,我宁愿失去它的头部。

Is there any way to use UIViewContentModeScaleAspectFill and center top left image corner with top left UIImageView corner? If I need to lose a part of image, I prefer to lose its feet that its head.

更新

UPDATE

我正在使用 sdWebImage 框架和我已经使用了Vadim Yelagin的答案,现在这是我的代码:

I'm using sdWebImage framework and I have used Vadim Yelagin answer and this is my code now:

[cell.photo setImageWithURL:[NSURL URLWithString:entry.photo]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                        success:^(UIImage *image) {
                            CGFloat scaleX = cell.photoBorder.bounds.size.width / image.size.width;
                            CGFloat scaleY = cell.photoBorder.bounds.size.height / image.size.height;
                            CGFloat scale = MAX(scaleX, scaleY);
                            cell.photo.frame = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);
                            cell.photo.image = image;
                        } 
                        failure:nil];

我必须等待所有图像加载并重新加载在UITableView周围移动才能看到正确的图像右单元格(这是因为图像被缓存并且加载速度更快)。

I have to wait to get all images loaded and reload move around UITableView to see the right image on the right cell (this is because images are cached and it loads faster).

而是使用 cell.photo.frame cell.photo.image 我需要传递给阻止 indexPath.row 以获得正确的单元格。

Instead using cell.photo.frame and cell.photo.image I need to pass to block indexPath.row to get the right cell.

我可以为UIImageView + WebCache添加一个新方法:

I could add a new method to UIImageView+WebCache:

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image, NSInteger row))success failure:(void (^)(NSError *error))failure;
{
    [self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure];
}

但如果我这样做,我会收到错误:

But if I do that, I get an error here:

[self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure];

因为现在成功的类型不是(void(^)(UIImage) * image))

Because now success isn't of type (void (^)(UIImage *image))

你知道如何解决这个问题吗?

Do you know how can I fix this problem?

推荐答案

Afaik,最简单的方法是根据图像的大小和您希望它适合的帧来手动计算imageView的帧。

Afaik, the easiest way is to calculate the imageView's frame manually from the size of the image and the frame you want it to fit in.

使用您当前为imageView指定的帧创建视图,将imageView作为其子视图。设置 view.clipsToBounds = YES

Create a view with the frame you're currently specifying for your imageView, make the imageView its subview. Set view.clipsToBounds = YES.

然后在分配图像时,执行类似

Then when you assign an image, do something like

CGFloat scaleX = view.bounds.size.width / image.size.width;
CGFloat scaleY = view.bounds.size.height / image.size.height;
CGFloat scale = MAX(scaleX, scaleY);
imageView.frame = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);
imageView.image = image;

这篇关于UIImageView,setClipsToBounds以及我的图像如何失去理智的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆