0
我有一個UIButton需要改變它的圖像,一旦用戶從相冊中選擇了一張照片來顯示它的選擇,我已經啓動並運行了這個設置,但是一旦用戶選擇了一張照片,我需要將它調整到按鈕的尺寸而這正是我需要幫助,這裏是我的代碼:如何調整自定義UIButton的圖像大小?
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *giftImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[giftImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
[giftButton setBackgroundImage:giftImage forState:UIControlStateNormal];
giftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
giftButton.imageView.contentMode = UIViewContentModeScaleToFill;
giftButton.imageView.bounds = CGRectMake(0, 0, 62, 62);
}
UPDATE: 找到了解決辦法,到目前爲止一切發生的是,按鍵得到了相應調整爲圖像的尺寸,但是使用下面的代碼,我設法在我需要的尺寸內插入圖像,這是一個正在進行的工作,但正在工作;)
CGSize targetSize = CGSizeMake(62, 62);
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = CGPointMake(0, 0);
thumbnailRect.size.width = 62;
thumbnailRect.size.height = 62;
[giftImage drawInRect:thumbnailRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
[giftButton setBackgroundImage:newImage forState:UIControlStateNormal];
UIGraphicsEndImageContext();
的代碼是從這個帖子Resize and crop images before displaying in UITableViewCells
謝謝,但我已經找到了解決辦法 –