防止在iOS中的Scrollview中拉伸图像

时间:2013-04-03 03:07:03

标签: ios uiscrollview uibutton imageview autoresizingmask

我在滚动视图中添加了几个按钮。我正在设置带图像的按钮背景。当您看到输出时,图像似乎会拉长。我想避免这种情况。

所以我试过这个:

创建了一个自定义视图,用于处理图像并使此图像远离边缘。

为自动调整部分做了这个:

enter image description here

我的代码如下所示:

for (int counter = 0; counter < imageCount; counter++) {

        NSString *imageName = [NSString stringWithFormat:@"%@_img0%d.jpg",mRoutePointId,counter+1];
        UIImage *infoImage = [UIImage imageNamed:imageName];

        GalleryImage *galleryItem = [[GalleryImage alloc] initWithImage:infoImage];

        UIButton *infoImageButton = [[UIButton alloc] initWithFrame:CGRectMake(xOffset, 0, infoImagesScrollView.frame.size.width, infoImagesScrollView.frame.size.height)];
        infoImageButton.tag = counter;
        [infoImageButton setImage:galleryItem.galleryImage forState:UIControlStateNormal];
        [infoImageButton setImage:galleryItem.galleryImage forState:UIControlStateHighlighted];
        [infoImageButton addTarget:self action:@selector(infoImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [infoImageButton setTitle:[NSString stringWithFormat:@"%@_img0%d.jpg",mRoutePointId,counter+1] forState:UIControlStateApplication];
        [infoImagesScrollView addSubview:infoImageButton];
        xOffset+=infoImagesScrollView.frame.size.width;
    }

CustomView.m

#import "GalleryImage.h"

@implementation GalleryImage

@synthesize galleryImage;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithImage:(UIImage*) image
{
    self = [super init];
    if (self) {
//        galleryImageView = nil;
        galleryImage = image;
    }
    return self;
}

-(void)awakeFromNib
{
    [galleryImageView setImage:galleryImage];
}

需要一些指导,说明如何在添加到按钮时确保图像不会拉伸。

欢迎提出其他建议..

1 个答案:

答案 0 :(得分:0)

这可能是图像视图的问题contentMode