没有调整大小,但在设置UIButton的backgroundImage时进行定位

时间:2013-08-02 02:41:12

标签: ios uibutton uiimage

我希望按钮基本上与原始图片的某个部分重叠并裁剪其余部分,而不是将图片压缩到按钮中。我似乎无法找到一个UIContentMode来实现这一点。

uibutton

2 个答案:

答案 0 :(得分:1)

试试这个:

你可以在任何UIViews中的一些CGRect之后拥有一个UIImage。只需使用包含原始UIImage的UIImageView部分制作CGRect,然后执行以下代码:

 CGRect newImageFrame = CGRectMake(...); //fill this rect according to the crop area of the imageView
 UIGraphicsBeginImageContextWithOptions(newImageFrame, YES, 0);
 [editingCell.layer renderInContext:UIGraphicsGetCurrentContext()];
 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

 UIGraphicsEndImageContext();

现在newImage将保留您将用作按钮背景的图像。

实施例: 假设您的UIImageView具有框架(50,50,100,200),您可以使用以下内容裁剪图像的类似结果:newImageFrame = CGRectMake(50,130,100,40)。

请告诉我是否有效。

答案 1 :(得分:0)

请尝试以下代码:

CGRect frame = CGRectMake(10, y, 280, 40);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = frame;
        button.tag=i;
        [button setTitle:(NSString *)@"new button" forState:(UIControlState)UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"temp.png"] forState:UIControlStateNormal];
        [self.view addSubview:button];

我已经检查过了。

希望这会对你有所帮助。