Incorrect size of Images in TableView
I am creating a table view with images from an URL开发者_C百科, but the images don´t re-size to all the view until I presset it in the row. any idea why that is happening? This is a custom tableview
My code is :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Celula_Noticias";
Celula_Noticias *cell = (Celula_Noticias*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray * top= [[NSBundle mainBundle] loadNibNamed:@"Celula_Noticias" owner:self options:nil];
for(id currentObject in top){
if ([currentObject isKindOfClass:[Celula_Noticias class]]) {
cell= (Celula_Noticias *) currentObject;
break;
}
}
}
cell.Image_Noticias_1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[noticias objectAtIndex:indexPath.row ] objectAtIndex:0]]]];
cell.Image_Noticias_2.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[noticias objectAtIndex:indexPath.row ] objectAtIndex:2]]]];
cell.Titulo_Noticias_1.text=[[noticias objectAtIndex:indexPath.row ] objectAtIndex:1];
cell.Titulo_Noticias_2.text=[[noticias objectAtIndex:indexPath.row ] objectAtIndex:3];
return cell;
}
the table is correctly fill, but the image dont start with the correct size.
I have tried use the CGRectMake but it still don t work.
Thanks.
Create a function to post process the downloaded image:
- (UIImage*)postProcessImage:(UIImage*)image
{
CGSize itemSize = CGSizeMake(50, 50);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image drawInRect:imageRect];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
The function above will give you back a nice image of 50*50.
Then you could do:
UIImage* downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"...url goes here..."]]];
UIImage* newImage = [self posProcessImage:downloadedImage];
cell.imageView.image=newImage
I think, the UIImageView
you are using, you should change it's property to aspect fit(if it is no so)
I have solve the problem... My view inside the tableviewcell in the Xib file was bigger than the tableviewcell, so it begins incorrectly (i don t now why), but now the table is correctly fill..
Thanks to all
精彩评论