為按鈕設(shè)置網(wǎng)絡(luò)圖片, 可以使用SDWebImage第三方庫提供的UIButton+WebCache.h 分類 提供的以下方法:
[btn sd_setImageWithURL:btnUrl forState:UIControlStateNormal];
#import "UIButton+WebCache.h"
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *btnUrl = [NSURL URLWithString:@"http://cdn2.jianshu.io/assets/web/logo-58fd04f6f0de908401aa561cda6a0688.png"];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(60, 120, 180, 44)];
[btn setBackgroundColor:[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1]];
[btn setTitle:@"進入簡書" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[btn sd_setImageWithURL:btnUrl forState:UIControlStateNormal];
[self.view addSubview:btn];
}
然而,使用了默認(rèn)的網(wǎng)絡(luò)圖片大小, 顯示起來卻是這樣的, 我嘗試了各種按鈕的屬性設(shè)置, 都沒有把這個妖孽改成合適大小. (包括嘗試了更改按鈕的imageEdgeInsets 屬性, 也沒成功.)
默認(rèn)網(wǎng)絡(luò)圖片大小是這樣的.png
然后我想到了這樣的方案: 下載下來的圖片, 先調(diào)整圖片的尺寸, 然后在去設(shè)置到按鈕上, 同樣, UIButton+WebCache.h 分類 提供的以下方法可以實現(xiàn):
//這個方法提供了完成回調(diào), 回調(diào)提供了下載下來的圖片,這樣我們就可以先更改這個圖片的尺寸, 然后在設(shè)置給按鈕使用.
[btn sd_setImageWithURL:btnUrl forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[btn setImage:image forState:UIControlStateNormal];
}];
```
然后有了以下代碼:
```
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *btnUrl = [NSURL URLWithString:@"http://cdn2.jianshu.io/assets/web/logo-58fd04f6f0de908401aa561cda6a0688.png"];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(60, 120, 180, 44)];
[btn setBackgroundColor:[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1]];
[btn setTitle:@"進入簡書" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[btn sd_setImageWithURL:btnUrl forState:UIControlStateNormal completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
image = [self imageByScalingToSize:CGSizeMake(80, 36) ForImage:image];
[btn setImage:image forState:UIControlStateNormal];
}];
[self.view addSubview:btn];
}
- (UIImage *)imageByScalingToSize:(CGSize)targetSize ForImage:(UIImage *)image{
//用作3倍率縮放, 保證圖片不失真
targetSize = CGSizeMake(targetSize.width*3, targetSize.height*3);
UIImage *sourceImage = image;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor < heightFactor)
scaleFactor = widthFactor;
else
scaleFactor = heightFactor;
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor < heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
} else if (widthFactor > heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
// this is actually the interesting part:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
UIImage *image3Scale = [[UIImage alloc] initWithData:UIImagePNGRepresentation(newImage) scale:3.0];
return image3Scale;
}
```
效果:
![設(shè)置圖片的尺寸后再設(shè)置到按鈕上.png](http://upload-images.jianshu.io/upload_images/1764130-82648e4cce363540.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)