UIImageView,setClipsToBounds以及我的图像如何失去理智

Van*_*nel 11 scaling uiimageview ios

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

我使用上的验证码UIImageViewUITableViewCell:

cell.photo.contentMode = UIViewContentModeScaleAspectFill;
[cell.photo setClipsToBounds:YES];
Run Code Online (Sandbox Code Playgroud)

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

然后,我需要使用setClipsToBounds:YES不允许这样,但现在我的所有图像都失去了他们的头脑.

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

UPDATE

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

[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];
Run Code Online (Sandbox Code Playgroud)

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

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

我可以为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];
}
Run Code Online (Sandbox Code Playgroud)

但如果我这样做,我在这里得到一个错误:

[self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure];
Run Code Online (Sandbox Code Playgroud)

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

你知道我怎么解决这个问题?

Fab*_*ser 0

没有内置方法可以执行此操作,但您可以手动执行此操作:

UIGraphicsBeginImageContext(CGSizeMake(desiredWidth, desiredHeight));

[originalImage drawAtPoint...];
...

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)