[英]resize image in table view cell
在表視圖單元格中調整圖像大小...我是新程序員
通過此代碼圖像進入單元格...但我想根據我的需要調整圖像大小....
如果可能的話,給我一點代碼...預先感謝...
- (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;
}
資料來源:
您可以像這樣更改imageView的框架:
CGRect frame = imageView.frame;
frame.size.height = 30.0;
/* other frame changes ... */
imageView.frame = frame;
你也應該看看的UIImageView contentMode屬性(見這個職位 )。 它使您可以控制圖像的繪制方式,它實際上是一個UIView屬性(因此UIImageView繼承了它)。 它為您提供以下選項:
指定視圖大小改變時如何調整其內容。
typedef enum {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
您應該使用單元格的backgroundView屬性並添加可拉伸圖像:
UIImage* backgroundImage = [[UIImage imageNamed:@”stretchableImage.png”] stretchableImageWithLeftCapWidth:44 topCapHeight:45];
希望對您有幫助,文森特
1°)那么,通過調整大小,您的意思是正確縮放? 您應該檢查UIImageView類的contentMode屬性[cell.imageView setContentMode:];
2°)如果要調整框架的大小,則只需執行[cell.imageView setFrame:CGRectMake()];
取決於您真正的問題是^^
祝好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.