iphone:把uiimage放在tableview节头,拉伸问题 [英] iphone: put uiimage in tableview section header, stretch problem

查看:138
本文介绍了iphone:把uiimage放在tableview节头,拉伸问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,人们,
我把UIImage放到我的UITableVIew的节头中。因此我使用了一些我在互联网上找到的代码:

   - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection :( NSInteger)section {

self.imageViewForImage.image = [helperClass resizeImage:[self.offerItem objectForKey:@picture] forSize:CGSizeMake(280.0,280.0)];

[self.imageViewForImage.layer setBorderColor:[[UIColor blackColor] CGColor]];
[self.imageViewForImage.layer setBorderWidth:1.2];

self.imageViewForImage.contentMode = UIViewContentModeBottom;
返回self.imageViewForImage;}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if( section == 0)
返回320;
返回1.0;
}

首先,图像完全按照iPhone的完整320px宽度进行缩放。但后来我发现,我的整个UITableView的contentMode-property被设置为UIViewContentModeScaleToFill。所以我添加了一行代码,告诉我的UIImageView不要进行缩放。在我的情况下,我将其设置为UIViewContentModeBottom。



问题:这一切都有效,图像最终以280x280的预期尺寸显示,但我做了边框图片周围仍然拉伸/调整大小到iphone的整个宽度......?!



我无法弄清楚如何解决这个问题,因为我想要UIImage周围的边界..



感谢您对此有任何帮助..



编辑:有没有人有一些想法,这里出了什么问题?

解决方案

您的UIImageView有一个图层,其中包含您为图像设置的内容。图层的大小等于imageView的大小,但图像的大小小于它。因此,当您将contentMode设置为视图时,如果内容大小小于图层大小,则会在其底层绘制图层内容。因此,即使您已调整其内容图像的大小,图层的大小(以及其框架的大小)仍然相同。



问题是您正在尝试调整大小图像,你应该调整UIImageView的大小。这部分代码看起来像这样:

   - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger )
{
UIView * headerView = [[UIView alloc] init];

self.imageViewForImage.frame = CGRectMake(20,20,280,280);
self.imageViewForImage.image = [self.offerItem objectForKey:@picture];
self.imageViewForImage.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

[self.imageViewForImage.layer setBorderColor:[[UIColor blackColor] CGColor]];
[self.imageViewForImage.layer setBorderWidth:1.2];

[headerView addSubview:self.imageViewForImage];
return [headerView autorelease];
}

希望这会有所帮助!



更新:问题是此方法返回的视图由tableView重新构建以填充所有标头。所以我们必须创建一个实际上是标题视图的视图,并在其上放置一个imageView。


hey people, I put an UIImage into the section header of my UITableVIew. Therefor I used some code I found on the internet:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

self.imageViewForImage.image = [helperClass resizeImage:[self.offerItem objectForKey: @"picture"] forSize:CGSizeMake(280.0, 280.0) ];

[self.imageViewForImage.layer setBorderColor: [[UIColor blackColor] CGColor]];
[self.imageViewForImage.layer setBorderWidth: 1.2];

self.imageViewForImage.contentMode = UIViewContentModeBottom;
return self.imageViewForImage;}


-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return 320;
    return 1.0;
}

At first the image was totally scaled over the complete 320px width of the iphone. But then I found out, that my whole UITableView's contentMode-property was set to "UIViewContentModeScaleToFill". So I added a line of code, which tells my UIImageView not to do the scaling. In my case I set it to "UIViewContentModeBottom".

Problem: This whole thing works, the image is finally shown in the aspected size of 280x280, BUT the border I did around the picture is still stretched / resized to the complete width of the iphone...?!

I can't figure out how to resolve this issue, because I want the border around the UIImage..

Thanks for any help on this..

edit: does anyone has some idea, what is going wrong here?

解决方案

Your UIImageView has a layer the contents of which you set to your image. The size of layer equals to the size of imageView, but size of image is less then it. So when you set contentMode to your view it says to it's layer to draw layers contents at bottom if contents size is less than layers size. So size of layer (and as consequence size of its frame) are still the same even though you have resized its contents image.

The problem is that you are trying to resize image, when you should resize UIImageView. This part of code should look something like this:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] init];

    self.imageViewForImage.frame = CGRectMake(20, 20, 280, 280);
    self.imageViewForImage.image = [self.offerItem objectForKey:@"picture"];
    self.imageViewForImage.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

    [self.imageViewForImage.layer setBorderColor: [[UIColor blackColor] CGColor]];
    [self.imageViewForImage.layer setBorderWidth: 1.2];

    [headerView addSubview:self.imageViewForImage];
    return [headerView autorelease];
}

Hope this'll help!

UPDATED: The issue was that view that this method returns is reframed by tableView to fill all the header. So we must make a view that will actually be a header view and place an imageView on it.

这篇关于iphone:把uiimage放在tableview节头,拉伸问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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