0

イメージビューを追加したテーブルビューセル[プロトタイプ]があります。次に、このイメージビューの高さと幅をサイズインスペクターで手動で設定します。しかし、私は使用しています

[cell viewWithTag:12345]

私のObjectiveCコードでビューポインタを取得し、画像ビューの画像を更新します。ただし、手動による制約が上書きされ、画像が独自のサイズになることがわかりました。属性インスペクターとプログラムの両方で、さまざまなモード(アスペクトフィル、アスペクトフィットなど)を試しました。

だから私の質問は、どうすれば制約を設定できますか?私はどこが間違っていますか?なぜ彼らは働いていないのですか?

4

3 に答える 3

3

画像の特定のさとには、次の方法を使用します

+ (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-03-12T10:41:13.137 に答える
0

次のコードは役に立ちます

ケース1:

   -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...

    return cell;
}

アウトプット: ここに画像の説明を入力してください

そしてCase2:

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...


    return cell;
}

出力: ここに画像の説明を入力してください

実際の画像サイズは460x246ですが、フレームによって違いがわかります

ありがとうございました

于 2013-03-12T10:54:22.320 に答える
0

まず、問題が画像ビューのcontentModeにあるのか、画像ビューのサイズにあるのかを確認します。自動サイズ変更マスクが原因で、イメージビューのサイズが変更される可能性もあります。画像ビューをで初期化する場合

- (id)initWithImage:(UIImage *)image

メソッドあなたのimageviewは自動的に画像のサイズにサイズ変更されます。

于 2013-03-12T10:41:24.513 に答える