-3

这是我的 cellForRowAtIndexPath: 方法,我用一些数据和图像填充 tableView。我正在尝试缩小图像的大小,使它们的大小都相同。每个单元格行都有两个图像。一个是所有行都相同的静态图像。这张图不错。这是我试图调整到某个尺寸的延迟加载的图像(cell.imageView.image)。

谢谢你的帮助

这是我的代码:

- (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;

}

4

1 回答 1

3

以下代码用于crop/resize UIImage,您只需传递所需的高度和宽度UIImage

对于获取裁剪图像:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need);
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

使用以下返回方法UIImage如您想要的图像大小

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }

在这里,您将获得通过上述方法返回的裁剪图像;

或调整大小

并且还使用以下方法来调整 UIImage的特定高度宽度

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

此方法返回NewImage,具有您想要的特定大小。

于 2013-08-02T09:39:30.937 回答