在iOS中防止在Scrollview中拉伸图像
我在scrollview中添加了几个按钮。我正在用图像设置按钮的背景。当您看到输出时,图像似乎被拉伸。我想避免这种情况 所以我试过这个: 创建了一个自定义视图,用于处理图像并使此图像远离边缘固定 对自动调整大小部件执行以下操作: 我的代码如下所示:在iOS中防止在Scrollview中拉伸图像,ios,uiscrollview,uibutton,imageview,autoresizingmask,Ios,Uiscrollview,Uibutton,Imageview,Autoresizingmask,我在scrollview中添加了几个按钮。我正在用图像设置按钮的背景。当您看到输出时,图像似乎被拉伸。我想避免这种情况 所以我试过这个: 创建了一个自定义视图,用于处理图像并使此图像远离边缘固定 对自动调整大小部件执行以下操作: 我的代码如下所示: for (int counter = 0; counter < imageCount; counter++) { NSString *imageName = [NSString stringWithFormat:@"%@_i
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;
}
需要一些关于如何确保图像添加到按钮时不会拉伸的指导
欢迎使用其他建议。这可能是图像视图的
contentMode
问题,例如,尝试galleryImageView.contentMode=UIViewContentModeCenter
我有横向和纵向图像。风景图片效果很好。别紧张。但肖像图像往往会被拉伸。放置在awakeFromNib中的galleryImageView.contentMode=UIViewContentModeCenter。。但还是没有改变。。。
#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];
}