应用场景:UI只提供了一张占位图,但是app内需要用到的尺寸各种各样,怎样办呢?
拉伸图片但是要保证不变形! 方法如下:
代码:
CGFloat top = 0.5;
CGFloat left = 0.5;
CGFloat bottom = 0.5;
CGFloat right = 0.5;
//上面四句是设置拉伸的尺寸, 代表了拉伸上下左右边缘的0.5距离
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
UIImage *img = [UIImage imageNamed:@"nanle"];
UIImageResizingMode mode = UIImageResizingModeStretch;
UIImage *newImage = [img resizableImageWithCapInsets:edgeInsets resizingMode:mode];
[self.img sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:newImage];//这句是我工程里的代码,网友自己替换成自己的