2

重複の可能性:
iPhoneのUI画像に丸い角を設定する方法

基本的に、これは画像プレビューを追加する方法ですUIScrollView。ボタンを追加してから画像を追加しますが、画像を角を丸くしてボタンの形状に適応させるにはどうすればよいですか。私のプレビューではこのように見えるからです。

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

- (void)addImage:(UIImage *)imageToAdd {
    [_images addObject:imageToAdd];
    [_thumbs addObject:[imageToAdd imageByScalingAndCroppingForSize:CGSizeMake(50, 50)]];

    int row = floor(([_thumbs count] - 1) / 5);
    int column = (([_thumbs count] - 1) - (row * 5));

    UIImage *thumb = [_thumbs objectAtIndex:[_thumbs count]-1];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(column*60+10, row*60+10, 55, 55);
    [button setImage:thumb forState:UIControlStateNormal];
    [button addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = [_images count] - 1;
    // This is the title of where they were created, so we can see them move.s
    [button setTitle:[NSString stringWithFormat:@"%d, %d", row, column] forState:UIControlStateNormal];

    [_buttons addObject:button];
    [scrollView addSubview:button];
    // This will add 10px padding on the bottom as well as the top and left.
    [scrollView setContentSize:CGSizeMake(300, row*60+20+60)];

}

また、私はこれを試しましたが、動作しません。

UIImageView * roundedView = [[UIImageView alloc] initWithImage: thumb];
// Get the Layer of any view
CALayer * l = [roundedView layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];

// You can even add a border
[l setBorderWidth:4.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
4

1 に答える 1

5

ボタンフレームでUIButtonの背景にimageViewを追加してみてください

[imgView setBackgroundColor:[UIColor clearColor]];
imgView.clipsToBounds = TRUE;
imgView.layer.cornerRadius = 20.0;//try different size for corner radious
imgView.layer.borderWidth = 0.0;// give size if you want to border for image

編集 :

問題に投稿したコードの代わりに、私の次のコードを使用してください

- (void)addImage:(UIImage *)imageToAdd {
    [_images addObject:imageToAdd];
    [_thumbs addObject:[imageToAdd imageByScalingAndCroppingForSize:CGSizeMake(50, 50)]];

    int row = floor(([_thumbs count] - 1) / 5);
    int column = (([_thumbs count] - 1) - (row * 5));

    UIImage *thumb = [_thumbs objectAtIndex:[_thumbs count]-1];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(column*60+10, row*60+10, 55, 55);
       [button setBackgroundColor:[UIColor clearColor]];
        //    [button setImage:thumb forState:UIControlStateNormal];

            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(column*60+10, row*60+10, 55, 55)];
        imgView.image =thumb;
        [imgView setBackgroundColor:[UIColor clearColor]];
        imgView.clipsToBounds = TRUE;
        imgView.layer.cornerRadius = 20.0;//try different size for corner radious
        imgView.layer.borderWidth = 0.0;// give size if you want to border for image
        [button addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = [_images count] - 1;
    // This is the title of where they were created, so we can see them move.s
    [button setTitle:[NSString stringWithFormat:@"%d, %d", row, column] forState:UIControlStateNormal];

    [_buttons addObject:button];
    [scrollView addSubview:button];
    // This will add 10px padding on the bottom as well as the top and left.
    [scrollView setContentSize:CGSizeMake(300, row*60+20+60)];

}

これがお役に立てば幸いです...

:)

于 2012-10-04T05:52:25.497 に答える