여기 내 cellForRowAtIndexPath : 메서드를 사용하여 tableView에 데이터와 이미지를 채우는 방법을 설명합니다. 나는 크기가 같아 지도록 이미지 크기를 줄이려고합니다. 각 셀 행에 대해 두 개의 이미지가 있습니다. 하나는 모든 행에 대해 동일한 정적 이미지입니다. 이 이미지는 괜찮습니다. lazily 로딩되는 이미지 (cell.imageView.image)입니다. 특정 크기로 크기를 조정하려고합니다. 방금 UIImage
의 높이와 너비를 통과해야 다음 코드UITableView 셀 이미지의 크기를 조절하는 방법
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
labelTitle.tag = 100;
labelTitle.textAlignment = UITextAlignmentLeft;
labelTitle.numberOfLines = 1;
labelTitle.font = [UIFont fontWithName:@"DIN-Bold" size:16];
[cell.contentView addSubview:labelTitle];
UILabel *labelDetail = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 320, 44)];
labelDetail.tag = 101;
labelDetail.textAlignment = UITextAlignmentLeft;
labelDetail.numberOfLines = 1;
labelDetail.font = [UIFont fontWithName:@"DIN-Regular" size:14];
[cell.contentView addSubview:labelDetail];
// cell image
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(250, 20, 36, 36)];
imgView.image = [UIImage imageNamed:@"someImage.png"];
imgView.contentMode = UIViewContentModeCenter;
imgView.alpha = 0.1;
[cell addSubview:imgView];
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
NSUInteger row = indexPath.row;
UILabel *labelTitle1 = (UILabel*)[cell viewWithTag:100];
labelTitle1.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"title"];
UILabel *labelDetail1 = (UILabel*)[cell viewWithTag:101];
labelDetail1.text = (NSString *)[[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"short_description"];
// set the image url & image caching
NSString *imageUrlString = [NSString stringWithFormat:@"%@", [[publicDataArray objectAtIndex:indexPath.row] objectForKey:@"image"]];
UIImage *cachedImage = [self.imageCache objectForKey:imageUrlString];
if (cachedImage)
{
cell.imageView.image = cachedImage;
}
else
{
cell.imageView.image = [UIImage imageNamed:@"rest_small.png"]; // initialize the image with placeholder image
// download in the image in the background
[self.imageDownloadingQueue addOperationWithBlock:^{
NSURL *imageUrl = [NSURL URLWithString:imageUrlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = nil;
if (imageData)
image = [UIImage imageWithData:imageData];
if (image)
{
[self.imageCache setObject:image forKey:imageUrlString]; // add the image to your cache
// finally, update the user interface in the main queue
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// make sure the cell is still visible
UITableViewCell *updateCell = [tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
cell.imageView.image = image;
}];
}
}];
}
return cell;
}
이미지 크기를 조정하기 만하면 기존 질문을 간단히 확인해야합니다. 다음은이 사이트 자체에 대한 그런 질문의 링크입니다. http://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uimimage – CodenameLambda1
나는 몇 가지 다른 것을 찾았지만 아무 소용이 없었습니다. . 그래서 여기에 내 시간을 너무 많이 쓰는 것을 피하기 위해 코드를 게시했습니다. – hanumanDev
검색 옵션에이 문자열 "ios image resize"를 입력하면 관련된 모든 게시물을 얻을 수 있습니다. – CodenameLambda1