2013-04-03 84 views
0

我正在向滾動視圖添加幾個按鈕。我正在用圖像設置按鈕的背景。當你看到輸出時,圖像似乎被拉長了。我想避免這一點。防止在iOS中的滾動視圖中拉伸圖像

所以我已經試過這樣:

創建一個自定義視圖,它處理圖像並做出這一形象固定遠離邊緣。

這樣做是爲了在自動尺寸部分:

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]; 
} 

需要就如何確保一些指導添加到按鈕時圖像不會伸展。

歡迎其他建議以及..

回答

0

它可能是圖像視圖的contentMode的問題。

+0

你能解釋一下嗎? – lakesh

+0

查看UIView'contentMode'下的文檔。 – matt

+0

例如嘗試'galleryImageView.contentMode = UIViewContentModeCenter' – matt