在表格 View 单元格中调整图像大小...我是新程序员
通过此代码图像进入单元格......但我想根据我的需要调整图像大小......
如果可能的话给我一些代码... 提前致谢...
- (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;
}
}
最佳答案
与其调整 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;
}
来源:
关于iphone - 在表格 View 单元格中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8026315/