0

右側のセルに表示する一連の画像を Web サーバーから読み込んでいます。ただし、画像のサイズが異なるため、リストが表示されたときに不均一に見えます。画像を 100 x 80 のような固定サイズに設定できる方法はありますか?

このセクションのコードは次のとおりです。

cell.lotImageView.image = [UIImage imageNamed:@"blankthumbnail.png"];
cell.lotImageView.clipsToBounds = YES;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
    //load image from web server
    NSString *strURL = [NSString stringWithFormat:@"%@/images/%@", user.url, lotPhoto[row]];
    NSURL *url = [[NSURL alloc] initWithString:strURL ];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];

    dispatch_async(dispatch_get_main_queue(), ^{
        // let's make sure the cell is still visible (i.e. hasn't scrolled off the screen)
        mainTableCell *cell = (mainTableCell *)[tableView cellForRowAtIndexPath:indexPath];
        if (cell)
        {
            cell.lotImageView.clipsToBounds = YES;
            cell.lotImageView.image =image;
        }
    });
});
4

2 に答える 2

1

画像に必要なサイズを実現する方法は次のとおりです。以下の関数をアプリケーションデリゲートに配置して、アプリケーション全体で使用できます。コードは次のとおりです。

+(UIImage *) resizeImage:(UIImage *)orginalImage resizeSize:(CGSize)size {

    CGFloat actualHeight = orginalImage.size.height;
    CGFloat actualWidth = orginalImage.size.width;
    if(actualWidth <= size.width && actualHeight<=size.height){
        return orginalImage;
        //NSLog(@"hi thassoods");
    }
    float oldRatio = actualWidth/actualHeight;
    float newRatio = size.width/size.height;
    if(oldRatio < newRatio){
        oldRatio = size.height/actualHeight;
        actualWidth = oldRatio * actualWidth;
        actualHeight = size.height;
    }
    else {
        oldRatio = size.width/actualWidth;
        actualHeight = oldRatio * actualHeight;
        actualWidth = size.width;
    }
    CGRect rect = CGRectMake(0.0,0.0,actualWidth,actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [orginalImage drawInRect:rect];
    orginalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return orginalImage;
}

ただし、指定した新しいサイズが画像の元のサイズに比例していない場合は、画像の解像度が適切に保たれない可能性があります。

ではごきげんよう!!!

于 2012-12-04T10:02:02.430 に答える
0

画像のサイズを変更してみてください。このコードはこれを作りました:

- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return destImage;
}
于 2012-12-04T09:58:13.537 に答える