我希望按钮基本上与原始图片的某个部分重叠并裁剪其余部分,而不是将图片挤压到按钮中。我似乎无法找到实现此目的的 UIContentMode。
最佳答案
试试这个:
您可以在任何 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/