我是iphone中的新手。现在开发iphone中的按钮图像视图。其中图像是显示大尺寸的图像我想调整图像大小。这意味着图像显示为中等大小。我实现这样的代码。
images = [[NSMutableArray alloc]init];
[images addObject:[UIImage imageNamed:@"bearlarge.jpg"]];
[images addObject:[UIImage imageNamed:@"bufflo_large.jpg"]];
[images addObject:[UIImage imageNamed:@"camel_large.jpg"]];
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
int row = 0;
int column = 0;
for(int i = 0; i < images.count; ++i) {
// UIImage *thumb = [images objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(column*105+14, row*105+10, 50,50);
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
//[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)];
self.view = view;
[view release];
任何人都可以发送代码来解释如何在iphone中调整按钮图像的大小。
先谢谢你。
答案 0 :(得分:1)
最好在按钮上添加UIImageView。这样你就可以轻松改变画面尺寸。
<强> CODE 强>
UIButton *imageBut=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
img.userInteractionEnabled=NO;
[imageBut addSubview:img];
[self.view addSubview:imageBut];
[imageBut release];
[img release];
否则,请使用
[button setBackgroundImage:YourImage];
答案 1 :(得分:1)
您可以为UIImage创建此类别:
- (UIImage *)rescaleImageToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resImage;
}
答案 2 :(得分:0)
setImage会将图像设置为原始大小,并将其包含在项目中。
setBackgroundImage将调整原始图像的大小。
[button setBackgroundImage:[images objectAtIndex:i] forState:UIControlStateNormal];
答案 3 :(得分:0)
-(void)scaleImage:(id)sender
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}