-3

これが私の cellForRowAtIndexPath: メソッドで、tableView にデータと画像を入力しています。画像のサイズを小さくして、すべて同じサイズにしようとしています。各セル行に 2 つの画像があります。1 つは、すべての行で同じ静的イメージです。この画像いいですね。特定のサイズにサイズを合わせようとしているのは、遅延して読み込まれている画像 ( 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;
    }

ここで、上記の方法で返された Croped Image を取得します。

またはサイズ変更

また、 Resizing 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 に答える