如何使 UITableView 不更改 imageView 的框架,或调整图像大小以填充框架大小? [英] How to make UITableView not change imageView's frame, or resize the image to fill the frame size?

查看:23
本文介绍了如何使 UITableView 不更改 imageView 的框架,或调整图像大小以填充框架大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tableView:cellForRowAtIndexPath 中,我将 imageView.image 设置为 1x1 或 60x60 占位符图像,而单元格的高度为 300.我还设置了 imageView.frame(0,0, 300, 300).

In tableView:cellForRowAtIndexPath, I am setting imageView.image to a 1x1, or 60x60 placeholder image, while the cell's height is 300. I also set imageView.frame to (0,0, 300, 300).

然后我调用一个方法 downloadImageFromURL:andSetIntoCell,它立即添加一个 UIActivityIndi​​catorView 并将框架设置为 (0,0, 300, 300)(下载是在一个线程中异步完成的,该方法不会等待下载而是立即返回).

Then I call a method downloadImageFromURL:andSetIntoCell, which immediately add a UIActivityIndicatorView and set the frame to (0,0, 300, 300) (the download is done asynchronously in a thread, and the method won't wait for the download but just return immediately).

然而,在tableView:cellForRowAtIndexPath返回单元格后,imageView.frame被调整为垂直居中原始60x60尺寸的图像.如果该占位符图像是 800x800,它实际上工作正常:框架仍然是 (0,0, 300, 300) 并且图像被缩小,但是当图像是 60x60 时,框架实际上被调整为类似 (0,120,60, 60),这没关系,因为占位符是透明的,但是 UIActivityIndi​​catorView 被添加为 imageView 的子视图,因此被垂直向下推,高度为300 并覆盖下一行的一部分.

However, after tableView:cellForRowAtIndexPath return the cell, imageView.frame is adjusted to vertically center the image of original 60x60 size. If that placeholder image is 800x800, it actually works fine: the frame is still (0,0, 300, 300) and the image is scaled down, but when the image is 60x60, the frame is actually adjusted to something like (0,120, 60, 60), that is ok because the placeholder is transparent, but the UIActivityIndicatorView was added as a subview of imageView, and therefore get pushed down vertically, having a height of 300 and cover up part of the next row.

有没有办法告诉 UITableView 不要改变 imageView.frame,或者通过编程轻松地将图像大小调整为 300x300(最好不要使用 BitmapContext),或者告诉 imageView 缩放以填充框架?

Is there a way to tell UITableView not to change imageView.frame, or easily resize the image to 300x300 programmatically (preferably not to use BitmapContext), or tell imageView to scale to fill the frame?

我补充说:

cell.imageView.contentMode = UIViewContentModeScaleToFill;

设置 imageView.frame 后,如果我在 UIView 上绘图,这通常有效,但 UITableView 仍会显示原始 60x60 大小,并将 imageView.frame 调整为垂直居中.

after setting imageView.frame, which normally works if I am drawing on a UIView, but UITableView will still show the original 60x60 size, and adjust imageView.frame to vertically center it.

(我想在这种情况下我可以让 UIActivityIndi​​catorView 图像的大小:60x60,但据说它是用 1x1 透明占位符图像来做的)

(I think in this case I can just make UIActivityIndicatorView the size of the image: 60x60, but supposedly it is to do it with a 1x1 transparent placeholder image)

更新:顺便说一句,我什至补充说:

self.table.autoresizesSubviews = NO;
self.table.autoresizingMask = UIViewAutoresizingNone;

cell.autoresizesSubviews = NO;
cell.autoresizingMask = UIViewAutoresizingNone;

cell.imageView.autoresizesSubviews = NO;
cell.imageView.autoresizingMask = UIViewAutoresizingNone;

在cell返回之前,也是一样:imageView被重新定位和调整大小.

before the cell is returned, and it is the same: the imageView is repositioned and resized.

推荐答案

cell 的 imageview 的框架在以后不会改变.所以使用layoutSubviews,我解决这个问题如下:-

The frames of cell's imageview is not changing later on. So using layoutSubviews, I resolve this matter as follows:-

使用名为 setImageWithURLRequest:AFNetworking 方法设置图像,我的示例使用 64*64 大小.您可以更改适用于您的表格视图的框架.

Set the image using AFNetworking's method named setImageWithURLRequest: I use the 64*64 size for my example. You can alter the frames which are applicable to your table view.

NSURL *urlMapThumbnail = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/staticmap?zoom=8&size=65x65&maptype=roadmap&markers=color:red%7Clabel:S%7C28.5002761,77.08126909999999"];

__weak UITableViewCell *cellTemp = cell;
[cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:urlMapThumbnail]
                      placeholderImage:[UIImage imageNamed:@"sotrelocation"]
                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                   CGSize finalSize = CGSizeMake(64, 64);
                                   image = [UIImage imageWithImage:image scaledToSize:finalSize];
                                   cellTemp.imageView.image = image;
                                   [cellTemp layoutSubviews];
                               } failure:nil];

//重新缩放图片大小的方法

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

这篇关于如何使 UITableView 不更改 imageView 的框架,或调整图像大小以填充框架大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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