在表格视图单元格中调整图像大小...我是新程序员
通过这个代码图像进入单元格...但我想根据我的需要调整大小图像....
如果可能的话,给我一点代码...... 提前谢谢......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];
if (cell == nil) {
//.......
}
search_items *pro = [result objectAtIndex:0];
if(indexPath.row==0)
{
NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.s_image ofType:@"png"];
UIImage *image=[UIImage imageWithContentsOfFile:filepath];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
//[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
[cell.imageView setFrame:CGRectMake(50,50,100,100)];
/*
CGRect iframe = cell.imageView.frame;
iframe.size.height = 100.0;
iframe.size.width=300.0;
cell.imageView.frame = iframe;
*/
cell.imageView.image=image;
}
}
答案 0 :(得分:3)
不是调整cell.imageView框架的大小,而是在将图像分配给cell.imageView.image之前尝试调整图像大小:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = @"My Cell's Title Text";
cell.detailTextLabel.text = @"My Cell's Description Text";
//### SETTING THE IMAGE HERE###
//Let's say I want to resize my image entitled "myImage.png" to be 50x50 pixels in the table cell
CGSize size = {50,50};
cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"myImage.png" scaledToSize:size];
return cell;
}
//Given a UIImage and a CGSize, this method will return a resized UIImage.
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
来源:
答案 1 :(得分:2)
您可以像这样更改imageView的框架:
CGRect frame = imageView.frame;
frame.size.height = 30.0;
/* other frame changes ... */
imageView.frame = frame;
您还应该查看UIImageView contentMode属性(请参阅this post)。它使您可以控制图像的绘制方式,实际上是UIView属性(因此UIImageView继承它)。它为您提供以下选项:
指定视图在其大小更改时如何调整其内容。
typedef enum {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
答案 2 :(得分:1)
您应该使用单元格的backgroundView属性并添加可伸缩的图像:
UIImage* backgroundImage = [[UIImage imageNamed:@”stretchableImage.png”] stretchableImageWithLeftCapWidth:44 topCapHeight:45];
希望这有帮助,文森特
答案 3 :(得分:0)
1°)嗯,通过调整大小,你的意思是哪个是正确缩放的? 您应该检查UIImageView类的contentMode属性 [cell.imageView setContentMode:];
2°)如果你想调整框架的大小,那就干嘛 [cell.imageView setFrame:CGRectMake()];
取决于你真正的问题^^
祝你好运。