[英]No resizing, but positioning when setting backgroundImage of UIButton
我希望按鈕基本上與原始圖片的某個部分重疊,然后裁剪其余部分,而不是將圖片壓縮到按鈕中。 我似乎找不到實現此目的的UIContentMode。
嘗試這個:
您可以在任何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)。
請讓我知道是否有效。
請嘗試以下代碼:
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];
我已經檢查了。
希望這會幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.