ios - 设置 UIButton 的 backgroundImage 时不调整大小,但定位

标签 ios uibutton uiimage

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

uibutton

最佳答案

试试这个:

您可以在任何 UIView 中的一些 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)。

请告诉我是否有效。

关于ios - 设置 UIButton 的 backgroundImage 时不调整大小,但定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18007894/

相关文章:

ios - 使用 UITableView registerNib : to load a custom UITableViewCell from nib? 时如何分配文件所有者

ios - 两种布局中的 Uibutton 约束

ios - Swift UIImageView 始终为空

ios - 如何删除UIImage背景颜色并使之透明?

iOS - 如何为头像创建分层图像

objective-c - 用它的所有 subview 给 UIView 着色

ios - InputAccessoryView 的 subview 工具栏按钮不起作用

iphone - 在 XCode 4 中为 iPhone 模拟器构建静态库

swift - 锚定到 InputAccessoryView 的按钮仅在其框架中工作

ios - 如何改变按钮的位置。 objective-c