imageView中的图像对齐问题 - iPhone

发布于 2024-08-04 05:33:14 字数 848 浏览 1 评论 0原文

我的应用程序中有以下代码:

// to set tip - photo in photo frame    
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;

if([data length]>0){
    cellThumbImg = [UIImage imageWithData:data];
} else {
    cellThumbImg = [UIImage imageNamed:@"130X90.gif"];
}

UIImageView *imgView = [[UIImageView alloc]initWithImage:cellThumbImg];

imgView.frame = photoFrame;

[imgBg setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:imgView];
//[cell.contentView sendSubviewToBack:imgView];

[imgView release];
[data release];

我想要以下内容:

  • 假设图像(通过 nsdata 加载)的大小为 60 x 60 那么内容模式应该为 UIVIewContentModeCenter
  • 假设图像(通过 nsdata 加载)具有更大的大小那么 60 x 60 那么内容模式应该是 UIViewContentModeScaleToFill。

但我的问题是如何确定通过 NSData 加载的图像的大小?

I have following code in my application:

// to set tip - photo in photo frame    
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;

if([data length]>0){
    cellThumbImg = [UIImage imageWithData:data];
} else {
    cellThumbImg = [UIImage imageNamed:@"130X90.gif"];
}

UIImageView *imgView = [[UIImageView alloc]initWithImage:cellThumbImg];

imgView.frame = photoFrame;

[imgBg setContentMode:UIViewContentModeScaleToFill];
[cell.contentView addSubview:imgView];
//[cell.contentView sendSubviewToBack:imgView];

[imgView release];
[data release];

I want the following:

  • Suppose an image ( which is loaded through nsdata ) is having size of 60 x 60 then content mode should be UIVIewContentModeCenter
  • Suppose an image ( which is loaded through nsdata ) is having more size then 60 x 60 then content mode should be UIViewContentModeScaleToFill.

But my question is how can I determine the size of the image loaded through NSData?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2024-08-11 05:33:14

您可以删除以下行,因为 UIImageView 将根据用于初始化它的图像调整自身大小:

imgView.frame=photoFrame;

从那里您可以通过获取 UIImageView 的大小来获取图像的大小代码>:

CGRect image_bounds = imgView.frame;

You can remove the following line because the UIImageView will size itself according to the image used to initialize it:

imgView.frame=photoFrame;

From there you can get the size of the image by taking the size of the UIImageView:

CGRect image_bounds = imgView.frame;
孤芳又自赏 2024-08-11 05:33:14

我尝试了以下代码&它起作用了。

// to set tip - photo in photo frame    
NSData *data=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;
if([data length]>0)
{ 
     cellThumbImg=[UIImage imageWithData:data];
} 
else 
{ 
     cellThumbImg=[UIImage imageNamed:@"130X90.gif"]; 
}
UIImageView *imgView=[[UIImageView alloc]initWithImage:cellThumbImg];
imgView.frame=photoFrame;

(cellThumbImg.size.height>60 || cellThumbImg.size.width>60 ) ? 
      [imgView setContentMode:UIViewContentModeScaleToFill] : 
      [imgView setContentMode:UIViewContentModeCenter] ;

[cell.contentView addSubview:imgView]; 
[imgView release]; [data release];

I tried following code & it worked.

// to set tip - photo in photo frame    
NSData *data=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
UIImage *cellThumbImg;
if([data length]>0)
{ 
     cellThumbImg=[UIImage imageWithData:data];
} 
else 
{ 
     cellThumbImg=[UIImage imageNamed:@"130X90.gif"]; 
}
UIImageView *imgView=[[UIImageView alloc]initWithImage:cellThumbImg];
imgView.frame=photoFrame;

(cellThumbImg.size.height>60 || cellThumbImg.size.width>60 ) ? 
      [imgView setContentMode:UIViewContentModeScaleToFill] : 
      [imgView setContentMode:UIViewContentModeCenter] ;

[cell.contentView addSubview:imgView]; 
[imgView release]; [data release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文