
问题
最近有需求,需要 button 使用 web image ,就直接使用了第三方库 YYKit 中提供的 category 方法
1
|
[_saveBtn setImageWithURL:[NSURL URLWithString:discountDic[@"pic"]] forState:UIControlStateNormal placeholder:nil];
|
但是这样子出现了问题,在屏幕像素比较大的设备上会出现显示问题,比如 6 plus

图片缩小了
解决方法
服务器上放的图片是 [email protected] 的图片,在显示图片时,直接将 [email protected] 的图片进行展示,图片展示 size 没有变化,但是像素变大了,所以图片会缩小
查看了第三方库,发现没有方法设置图片的 scale 比例,所以查看系统方法,发现系统中有对 scale 进行修改的方法
1 2
|
+ (nullable UIImage *)imageWithData:(NSData *)data scale:(CGFloat)scale NS_AVAILABLE_IOS(6_0); + (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation NS_AVAILABLE_IOS(4_0);
|
对原先代码进行修改,增加 scale,
1 2
|
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:discountDic[@"pic"]]] scale:2]; [_saveBtn setImage:image forState:UIControlStateNormal];
|
完成

近期评论