How to make UITableView not change imageView's frame, or resize the image to fill the frame size?

巧了我就是萌 提交于 2020-01-13 16:25:12

问题


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).

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).

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.

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?

I added:

cell.imageView.contentMode = UIViewContentModeScaleToFill;

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.

(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)

Update: by the way, I even added:

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

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

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

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


回答1:


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

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];

//Method to re-scale the image size

+ (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;
}


来源:https://stackoverflow.com/questions/12212565/how-to-make-uitableview-not-change-imageviews-frame-or-resize-the-image-to-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!